【发布时间】:2016-06-07 22:44:39
【问题描述】:
我们有一个 Spring Boot 1.3.2/Webflow Web 应用程序,我们将其转换为使用 SSO。我已按照“将 OAuth2 应用程序从 Spring Boot 1.2 迁移到 1.3”博客中的步骤进行操作,并将应用程序移交给我们的 Auth 服务器进行身份验证,并让 Web 应用程序使用令牌正确填充其安全上下文。
唯一不起作用的是我们拥有的自定义身份验证成功处理程序,它会在用户会话中配置一些位,然后再继续访问其登录页面。
目前在我们的安全配置中配置如下,它扩展了 WebSecurityConfigurerAdapter
@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 是可能的:
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;
}
}
有没有更好的方法来配置它?
【问题讨论】:
-
我发现 Spring 稍后运行
SsoSecurityConfigurer.configure(HttpSecurity http)并在那里创建新的OAuth2ClientAuthenticationProcessingFilter来处理成功和失败重定向。至于现在我不知道如何以“正确”的方式改变它,因为似乎没有任何东西来自 beanFactory。
标签: java spring-boot spring-security-oauth2