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

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);
    }
*/

}
  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-09 04:18
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    要解决Spring Security中不允许直接访问特定URL的问题,你需要在配置文件中添加deny标签来指定不允许哪些请求路径。以下是如何实现这一目标的方法:

    @Configuration
    @EnableWebSecurity
    public class MySecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Autowired
        private UserDetailsService userDetailsService;
        
        @Autowired
        private PasswordEncoder passwordEncoder;
        
        @Autowired
        private AuthenticationManager authenticationManager;
        
        @Autowired
        private LoginFailureHandler loginFailureHandler;
    
        @Autowired
        private LogoutSuccessHandler logoutSuccessHandler;
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .csrf().disable()
                    .cors().disable()
                    .authorizeRequests()
                    .antMatchers("/**").permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin()
                    .loginPage("/login")
                    .defaultSuccessUrl("/", false)
                    .permitAll()
                    .and()
                    .logout()
                    .logoutSuccessUrl("/")
                    .permitAll();
                    
            // 如果你想允许某些特定URL,可以在这里添加它们
            deny("/*");
            
            // 在这里配置其他任何你想要禁止的请求路径
            deny("/*");
            
            // 配置用户详细信息
            http.userDetailsService(this::loadUserDetails);
    
            // 这里是你的密码编码器
            http.passwordEncoder(passwordEncoder);
            
            // 这里是你的身份验证管理器
            http.authenticationManager(authenticationManager);
            
            // 这里是你需要处理的错误处理器
            http.exceptionHandling().authenticationEntryPoint(loginFailureHandler).logoutSuccessHandler(logoutSuccessHandler);
            
            // 这里是你的登录失败处理器
            http.loginFailureHandler(loginFailureHandler);
        }
    }
    
    private UserDetails loadUserDetails(UserDetails userDetails) {
        return userDetails;
    }
    

    在这个例子中,我们首先定义了一个名为MySecurityConfig的类,它继承了WebSecurityConfigurerAdapter。然后,我们在configure方法中使用了deny标签来阻止访问所有URL。

    接下来,我创建了一些注解和字段,以便你在运行时能够访问这些值。请注意,这个示例仅用于说明目的,并且可能不适合您的实际需求。

    最后,我还提供了一个loadUserDetails方法,如果你需要从数据库或其他数据源加载用户的详细信息。

    注意:这个示例并没有包含如何正确地获取用户详细信息或进行数据库操作的部分,这取决于你的具体业务逻辑。

    评论

报告相同问题?