Springboot + Spring Security 实现前后端分离登录认证及权限控制

前言
本文主要的功能
文章目录
文章正文
一、准备工作
1、统一错误码枚举
2、统一json返回体
3、返回体构造工具
4、pom
5、配置文件
二、数据库表设计
建表语句
初始化表数据语句
三、Spring Security核心配置:WebSecurityConfig
四、用户登录认证逻辑:UserDetailsService
1、创建自定义UserDetailsService
2、准备service和dao层方法
(1)根据用户名查询用户信息
(2)根据用户名查询用户的权限信息
五、用户密码加密
六、屏蔽Spring Security默认重定向登录页面以实现前后端分离功能
1、实现登录成功/失败、登出处理逻辑
(1)登录成功
(2)登录失败
(3)登出
2、在WebSecurityConfig中的configure(HttpSecurity http)方法中声明
八、会话管理(登录过时、限制单用户或多用户登录等)
1、限制登录用户数量
2、处理账号被挤下线处理逻辑
3、在WebSecurityConfig中声明
九、实现基于JDBC的动态权限控制
1、权限拦截器
2、安全元数据源FilterInvocationSecurityMetadataSource
3、访问决策管理器AccessDecisionManager
4、在WebSecurityConfig中声明
十、最终的WebSecurityConfig配置
十一、结束语
————————————————
版权声明:本文为CSDN博主「I_am_Rick_Hu」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/I_am_Hutengfei/article/details/100561564

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot和Vue前后端分离的解决方案是非常常见的,而SpringSecurity则是SpringBoot中处理权限问题的最佳解决方案之一。下面介绍一下如何使用SpringSecurity完美处理权限问题。 1. 引入SpringSecurity依赖 在pom.xml中引入SpringSecurity依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 配置SpringSecuritySpringBoot应用中,可以通过继承WebSecurityConfigurerAdapter类来配置SpringSecurity,代码如下: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomUserService customUserService; @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/", "/index").permitAll() .antMatchers("/admin/**").hasRole("ADMIN") .anyRequest().authenticated() .and() .formLogin().loginPage("/login").defaultSuccessUrl("/home").permitAll() .and() .logout().logoutUrl("/logout").logoutSuccessUrl("/login").permitAll(); } @Override public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(customUserService).passwordEncoder(new BCryptPasswordEncoder()); } @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/static/**"); } } ``` 3. 自定义用户认证 在上面的代码中,我们使用了自定义的用户认证服务CustomUserService。CustomUserService实现了UserDetailsService接口,代码如下: ```java @Service public class CustomUserService implements UserDetailsService { @Autowired private UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findByUsername(username); if (user == null) { throw new UsernameNotFoundException("用户名不存在!"); } List<GrantedAuthority> authorities = new ArrayList<>(); for (Role role : user.getRoles()) { authorities.add(new SimpleGrantedAuthority(role.getName())); } return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), authorities); } } ``` 在上面的代码中,我们使用了自定义的UserRepository来获取用户信息,然后将用户的角色转换成GrantedAuthority对象,返回给SpringSecurity。 4. 在Vue中处理权限问题 在Vue中,我们可以通过Vue Router的导航守卫来处理权限问题。代码如下: ```javascript import router from './router' import store from './store' router.beforeEach((to, from, next) => { if (to.meta.requireAuth) { // 判断该路由是否需要登录权限 if (store.state.token) { // 获取当前的token是否存在 next() } else { next({ path: '/login', query: {redirect: to.fullPath} // 将跳转的路由path作为参数,登录成功后跳转到该路由 }) } } else { next() } }) ``` 在上面的代码中,我们通过store来获取当前用户的token,如果token存在则允许访问,否则跳转到登录页面。 以上就是使用SpringSecurity处理权限问题的完整解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值