spring boot + spring security 基于 前端 ajax Content-Type为applications-json的 post方式的登陆 以及权限认证

1,maven依赖 加入

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

**

2,自定义WebSecurityConfigurerAdapter 的实现类

**

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private MyFilterSecurityInterceptor myFilterSecurityInterceptor;
@Autowired
private UserService userService;


@Autowired
private AuthenticationAccessDeniedHandler accessDeniedHandler;

@Override
protected void configure(HttpSecurity http) throws Exception {
     http.formLogin()  
      .loginPage("/user/usercontroller/login_page")
      .loginProcessingUrl("/user/usercontroller/login").permitAll()
      .and()
      .authorizeRequests()    // 定义哪些URL需要被保护、哪些不需要被保护
      .antMatchers("/user/usercontroller/login_page").permitAll()   // 设置所有人都可以访问的路径
      .anyRequest()        // 任何请求,登录后可以访问
      .authenticated()
      .and()
      .csrf().disable();     // 关闭csrf防护
      http.addFilterBefore(myFilterSecurityInterceptor,FilterSecurityInterceptor.class);
        //用重写的Filter替换掉原有的UsernamePasswordAuthenticationFilter
      http.addFilterAt(customAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class);
      http.exceptionHandling().accessDeniedHandler(accessDeniedHandler);

}

//注册自定义的UsernamePasswordAuthenticationFilter
@Bean
CustomAuthenticationFilter customAuthenticationFilter() throws Exception {
    CustomAuthenticationFilter filter = new CustomAuthenticationFilter();
    filter.setAuthenticationSuccessHandler(new SuccessHandler());
    filter.setAuthenticationFailureHandler(new FailureHandler());
    filter.setFilterProcessesUrl("/user/usercontroller/login");
    //这句很关键,重用WebSecurityConfigurerAdapter配置的AuthenticationManager,不然要自己组装AuthenticationManager
    filter.setAuthenticationManager(authenticationManagerBean());
    return filter;
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
   auth.userDetailsService(userService).passwordEncoder(new PasswordEncoder() { 
       @Override
       public String encode(CharSequence rawPassword) {
           return Md5Utils.encryptPassword((String) rawPassword, (String) rawPassword);
       }

       @Override
       public boolean matches(CharSequence rawPassword, String encodedPassword) {
           return encodedPassword.equals(Md5Utils.encryptPassword((String) rawPassword,(String) rawPassword));
       }
   });

}

}

3,继承 UserDetailsService 并实现认证方法

 @Service
 public class UserServiceImpl implements UserService,UserDetailsService {

@Autowired
UserMapper u
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值