【发布时间】:2018-08-09 02:44:17
【问题描述】:
我希望能够在用户未登录时控制自动重定向到 oauth2 授权服务器。
我生成了一个 JHipster Gateway 项目,下面的代码只是它的一个副本,添加了oAuth2ClientContextFilter 变量,即autowired,然后我将它用于setRedirectStrategy
但是,当需要使用它时,变量是NULL。我做错了什么?
@EnableOAuth2Sso
@Configuration
public class OAuth2SsoConfiguration extends WebSecurityConfigurerAdapter {
private final RequestMatcher authorizationHeaderRequestMatcher;
private final CorsFilter corsFilter;
@Autowired
private OAuth2ClientContextFilter oAuth2ClientContextFilter;
private final Logger log = LoggerFactory.getLogger(OAuth2SsoConfiguration.class);
public OAuth2SsoConfiguration(@Qualifier("authorizationHeaderRequestMatcher")
RequestMatcher authorizationHeaderRequestMatcher, CorsFilter corsFilter) {
this.authorizationHeaderRequestMatcher = authorizationHeaderRequestMatcher;
this.corsFilter = corsFilter;
oAuth2ClientContextFilter.setRedirectStrategy(new RedirectStrategy() {
@Override
public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {
// My Code Here
}
});
}
@Bean
public AjaxLogoutSuccessHandler ajaxLogoutSuccessHandler() {
return new AjaxLogoutSuccessHandler();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.addFilterBefore(corsFilter, CsrfFilter.class)
.headers()
.frameOptions()
.disable()
.and()
.logout()
.logoutUrl("/api/logout")
.logoutSuccessHandler(ajaxLogoutSuccessHandler())
.and()
.requestMatcher(new NegatedRequestMatcher(authorizationHeaderRequestMatcher))
.authorizeRequests()
.antMatchers("/api/profile-info").permitAll()
.antMatchers("/api/**").authenticated()
.antMatchers("/management/health").permitAll()
.antMatchers("/management/**").hasAuthority(AuthoritiesConstants.ADMIN)
.anyRequest().permitAll();
}
}
【问题讨论】:
标签: java spring-security oauth-2.0 jhipster spring-security-oauth2