【问题标题】:Redirect in a filter with Spring Boot使用 Spring Boot 在过滤器中重定向
【发布时间】:2015-06-29 06:06:49
【问题描述】:

在我的配置中 Spring Boot WebSecurityConfig 有一个过滤器,我需要查看用户是否有过期的密码,如果在应用程序上启用了它..

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    IdentityService userDetailsService;

    @Autowired
    AccountFilter accountFilter;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf()
                .and()
                .authorizeRequests()
                .antMatchers("/login", "/recover-credntial",
                        "/logout", "/resources/**").permitAll()
                .and()
                .formLogin()
                .loginPage("/login").failureUrl("/login?error")
                .usernameParameter("username")
                .passwordParameter("password")
                .permitAll()
                .and()
                .logout()
                .logoutSuccessUrl("/login?logout")
                .permitAll()
                .and()
                .exceptionHandling()
                .accessDeniedPage("/403")
                .and().addFilterAfter(accountFilter, UsernamePasswordAuthenticationFilter.class);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder authManagerBuilder)
            throws Exception {
        authManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        PasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder;
    }

}

如您所见,我在 HTTP 配置中有 .and().addFilterAfter(Account Filter, UsernamePasswordAuthenticationFilter.class);
如何定义我的过滤器,以便它可以重定向到我的某些控制器的 URL? 我在 Java Web 应用程序“Spring Boot”中使用 Java 配置,而不是文件 xml!

【问题讨论】:

    标签: spring servlets redirect filter spring-boot


    【解决方案1】:

    一种方法如下使用ExceptionMappingAuthenticationFailureHandler。这将意味着不使用 servlet。

    配置

    @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .antMatchers("/", "/home").permitAll()
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/login")
                    .permitAll()
                    .failureHandler(authenticationFailureHandler())
                    .and()
                .logout()
                    .permitAll();
        }
    

    身份验证失败处理程序

     @Bean
        public AuthenticationFailureHandler authenticationFailureHandler() {
            ExceptionMappingAuthenticationFailureHandler exceptionMappingAuthenticationFailureHandler = new ExceptionMappingAuthenticationFailureHandler();
            Map<String, String> exMap = new HashMap<String, String>();
            exMap.put("org.springframework.security.authentication.CredentialsExpiredException","/loginerror/credentialsexpired.htm");
            exceptionMappingAuthenticationFailureHandler.setExceptionMappings(exMap);
            return exceptionMappingAuthenticationFailureHandler;
        }
    

    自定义用户详细信息服务

    @Service
    public class CustomUserDetailsService implements UserDetailsService {
    
        @Autowired
        private UserRepository userRepository; 
    
        @Override
        public UserDetails loadUserByUsername(String username)
                throws UsernameNotFoundException {
            // Check if Password Expired then throw 
            throw new CredentialsExpiredException("Expired");
        }
    
    }
    

    【讨论】:

    • 我还没有测试过这个解决方案。但是这种方法是基于这个问题..stackoverflow.com/questions/8470173/…
    • 谢谢,但我需要一个过滤器,已经尝试了过滤器、GenericFilterBean 的各种实现……但是当我尝试运行 sendRedirect 时出现错误。这就是我得到错误的实现。
    • public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {HttpServletResponse httpResponse = (HttpServletResponse) response;身份 currentUser = identityService.findIdentityByUsername(authenticationFacade.getAuthentication().getName()); if (!currentUser.isAccountLocked()) {httpResponse.sendRedirect("403.html");}}chain.doFilter(request, httpResponse);
    猜你喜欢
    • 2020-07-27
    • 2017-08-23
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 2015-05-22
    相关资源
    最近更新 更多