赖小伙 2019-04-26 23:47 采纳率: 42.9%
浏览 3686

springSecurity 设置不需要登入url无效

springboot2.1.3 集成springSecurity设置了不需要登入的url无效,全部访问没有登入都跳转到未登入的handler里面。求助哇。


@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    private static final String[] AUTH_WHITELIST = {
            "/api/login",
            "/user/list"
    };
    @Autowired
    private AjaxAuthenticationEntryPointHandler authenticationEntryPoint;  //  未登陆时

    @Autowired
    private AjaxAuthenticationSuccessHandler authenticationSuccessHandler;  // 登录成功

    @Autowired
    private AjaxAuthenticationFailureHandler authenticationFailureHandler;  //  登录失败

    @Autowired
    private AjaxLogoutSuccessHandler logoutSuccessHandler;  // 注销成功

    @Autowired
    private AjaxAccessDeniedHandler accessDeniedHandler;    // 无权访问

    @Autowired
    private UserLoginService userLoginService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 加入自定义的安全认证
        auth.userDetailsService(userLoginService).passwordEncoder(new BCryptPasswordEncoder() {
            @Override
            public String encode(CharSequence rawPassword) {
                return SecurityUtils.encryptPassword((String) rawPassword);
            }

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

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()// 去掉 CSRF
                .anonymous().disable()
                .httpBasic()
                .and()
        //开启登录
                .formLogin()
                .successHandler(authenticationSuccessHandler) // 登录成功
                .failureHandler(authenticationFailureHandler) // 登录失败
                .permitAll()
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint) //没有登入时候的回调函数
                .accessDeniedHandler(accessDeniedHandler)// 无权访问 JSON 格式的数据
                .and()
                .logout()
                .logoutSuccessHandler(logoutSuccessHandler)
                .permitAll()
                .and()
                .authorizeRequests()
                .antMatchers("/user/list").permitAll()
                .anyRequest().authenticated()
        ;

    }
/*

    @Override
    public void configure(WebSecurity web) throws Exception {
        //静态资源不拦截
      //  web.ignoring().antMatchers(AUTH_WHITELIST);
    }
*/

}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?
    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 路易威登官网 里边的参数逆向
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
    • ¥50 需求一个up主付费课程