首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Spring Boot 1.3.2 (不带spring-cloud- AuthenticationSuccessHandler )和@EnableOAuth2Sso配置安全

使用Spring Boot 1.3.2 (不带spring-cloud- AuthenticationSuccessHandler )和@EnableOAuth2Sso配置安全
EN

Stack Overflow用户
提问于 2016-02-25 16:59:30
回答 1查看 11.1K关注 0票数 7

我们有一个Spring Boot 1.3.2/Webflow web应用程序,我们正在将其转换为使用SSO。我遵循了“将OAuth2应用程序从Spring Boot1.2迁移到1.3”博客中的步骤,让应用程序移交给我们的身份验证服务器进行身份验证,并让web应用程序使用令牌正确地填充其安全上下文。

唯一不起作用的部分是我们拥有的自定义身份验证成功处理程序,它在用户继续登录页面之前配置用户会话中的一些位。

目前,在扩展WebSecurityConfigurerAdapter的安全配置中,该配置如下所示

代码语言:javascript
复制
@Override
protected void configure(HttpSecurity http) throws Exception {
    // These are all the unprotected endpoints.
    http.authorizeRequests()
            .antMatchers(new String[] { "/", "/login", "/error",
                    "/loginFailed", "/static/**" })
            .permitAll();

    // Protect all the other endpoints with a login page.
    http.authorizeRequests().anyRequest()
            .hasAnyAuthority("USER", "ADMIN").and().formLogin().loginPage("/login").failureUrl("/loginFailed")
            .successHandler(customAuthenticationSuccessHandler()).and().logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
    http.exceptionHandling().accessDeniedHandler(new AccessDeniedHandler() {

        @Override
        public void handle(HttpServletRequest request, HttpServletResponse response,
                AccessDeniedException accessDeniedException) throws IOException, ServletException {
            if (accessDeniedException instanceof CsrfException) {
                response.sendRedirect(request.getContextPath() + "/logout");
            }
        }
    });
}

我可以看到处理程序在启动期间被配置,但是一旦用户成功登录,它就再也不会被调用。我在这个主题上发现的所有问题都涉及到使用OAuth2SsoConfigurerAdapter,然而,由于我们不再使用spring-cloud-security,因此这个类不可用。

更新:我发现使用BeanPostProcessor可以做到这一点:

代码语言:javascript
复制
public static class DefaultRolesPrefixPostProcessor implements BeanPostProcessor, PriorityOrdered {

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof FilterChainProxy) {

                FilterChainProxy chains = (FilterChainProxy) bean;

                for (SecurityFilterChain chain : chains.getFilterChains()) {
                    for (Filter filter : chain.getFilters()) {
                        if (filter instanceof OAuth2ClientAuthenticationProcessingFilter) {
                            OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationProcessingFilter = (OAuth2ClientAuthenticationProcessingFilter) filter;
                            oAuth2ClientAuthenticationProcessingFilter
                                    .setAuthenticationSuccessHandler(customAuthenticationSuccessHandler());
                        }
                    }
                }
            }
            return bean;
        }
    }

有没有更好的方法来配置它呢?

EN

回答 1

Stack Overflow用户

发布于 2016-08-27 16:52:00

如果您遵循Dave Syers优秀的Spring boot oauth2 tutorial,那么您最终将得到一个返回ssoFilter的方法

我向此筛选器添加了一个setAuthenticationSuccessHandler

代码语言:javascript
复制
@Autowired
private CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler;


private Filter ssoFilter() {
    OAuth2ClientAuthenticationProcessingFilter facebookFilter = new OAuth2ClientAuthenticationProcessingFilter("/login/facebook");
    OAuth2RestTemplate facebookTemplate = new OAuth2RestTemplate(facebook(), oauth2ClientContext);
    facebookFilter.setRestTemplate(facebookTemplate);
    facebookFilter.setTokenServices(new UserInfoTokenServices(facebookResource().getUserInfoUri(), facebook().getClientId()));
    facebookFilter.setAuthenticationSuccessHandler(customAuthenticationSuccessHandler);
    return facebookFilter;
} 

我的CustomAuthenticationSuccessHandler只是一个扩展了AuthenticationSuccessHandler的组件

代码语言:javascript
复制
@Component
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                    Authentication authentication) throws IOException, ServletException {
    //implementation
}

}

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35622563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档