springboot整合spring-security实现认证授权

1.认证授权数据库中的五张表并使用mybatisplus一一映射。

2.使用代码生成器生成生成对应的dao层、service层、impl、domain。

3.实现userDetails接口;SpringSecurity用户的实体;重写方法。

4.生成token的工具类。

5、jwt配置类。


6、统一返回结果result


7、各种登录登出返回结果处理。


8、其中登陆成功要进行token的生成。


9、实现userDetailsService接口。根据用户名查询详细的用户信息。


10、编写自定义登录验证。(springsecurity的核心业务处理功能) UserAuthenticationProvider implements AuthenticationProvider
      最后返回:return new UsernamePasswordAuthenticationToken(userInfo, password, authorities);


11、编写自定义权限注解验证(实现PermissionEvaluator接口)


12、编写JWT接口请求校验拦截器(extends BasicAuthenticationFilter);
      带token的请求和不带token的请求,返回不同结果,带token的请求要验证token的正确性。


13、编写最最最重要的spring-security核心配置类。
        继承 WebSecurityConfigurerAdapter  
        重写configure(AuthenticationManagerBuilder auth) 配置自定义的登录认证逻辑。返回8中自定义登录验证的逻辑。
        重写configure(HttpSecurity http)  配置 security的控制逻辑。

 authorizeRequests().antMatchers(JWTConfig.antMatchers.split(",")).permitAll()   // 不进行权限验证的请求或资源(从配置文件中读取)
        .anyRequest().authenticated() //其他的所有路径都需要进行认证授权。
                .and()
                // 配置未登录自定义处理类
                .httpBasic().authenticationEntryPoint(userAuthenticationEntryPointHandler)
                .and()
                // 配置登录地址
                .formLogin()
                .loginProcessingUrl("/login/userLogin")
                // 配置登录成功自定义处理类
                .successHandler(userLoginSuccessHandler)
                // 配置登录失败自定义处理类
                .failureHandler(userLoginFailureHandler)
                .and()
                // 配置登出地址
                .logout()
                .logoutUrl("/login/userLogout")
                // 配置用户登出自定义处理类
                .logoutSuccessHandler(userLogoutSuccessHandler)
                .and()
                // 配置没有权限自定义处理类
                .exceptionHandling().accessDeniedHandler(userAuthAccessDeniedHandler)
                .and()
                // 开启跨域
                .cors()
                .and()
                // 取消跨站请求伪造防护
                .csrf().disable();
        // 基于Token不需要session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        // 禁用缓存
        http.headers().cacheControl();
        // 添加JWT过滤器
        http.addFilter(new JWTAuthenticationTokenFilter(authenticationManager()));


      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值