我正在学习本教程:https://spring.io/guides/tutorials/spring-boot-oauth2/
我不知道如何在成功登录后重定向到我的html页面。
这是我的起点:
@SpringBootApplication
@EnableOAuth2Sso
public class SimpleApplication extends WebSecurityConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(SimpleApplication.class, args);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/login**", "/webjars/**", "/error**")
.permitAll()
.anyRequest()
.authenticated();
}
}
这是我的application.yml
security:
oauth2:
client:
clientId: id
clientSecret: secret
accessTokenUri: https://graph.facebook.com/oauth/access_token
userAuthorizationUri: https://www.facebook.com/dialog/oauth
tokenName: oauth_token
authenticationScheme: query
clientAuthenticationScheme: form
resource:
userInfoUri: https://graph.facebook.com/me
我尝试将下面的内容添加到configure方法中,但这只会带来更多的问题,比如缺少依赖项、缺少bean等等
.and()
.oauth2Login().defaultSuccessUrl("/after");
有人能给点建议吗?
发布于 2019-03-03 23:43:43
似乎spring安全自动配置中没有属性,所以您需要自己初始化过滤器,并在其中设置成功处理程序,下面是github中的链接
@SpringBootApplication
@Slf4j
@EnableOAuth2Sso
public class StackOverflowApplication extends WebSecurityConfigurerAdapter {
private AuthenticationSuccessHandler successHandler() {
return new SimpleUrlAuthenticationSuccessHandler("/after");
}
private OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationProcessingFilter() {
OAuth2SsoProperties sso = (OAuth2SsoProperties)this.getApplicationContext().getBean(OAuth2SsoProperties.class);
OAuth2RestOperations restTemplate = ((UserInfoRestTemplateFactory)this.getApplicationContext().getBean(UserInfoRestTemplateFactory.class)).getUserInfoRestTemplate();
ResourceServerTokenServices tokenServices = (ResourceServerTokenServices)this.getApplicationContext().getBean(ResourceServerTokenServices.class);
OAuth2ClientAuthenticationProcessingFilter filter = new OAuth2ClientAuthenticationProcessingFilter(sso.getLoginPath());
filter.setRestTemplate(restTemplate);
filter.setTokenServices(tokenServices);
filter.setApplicationEventPublisher(this.getApplicationContext());
filter.setAuthenticationSuccessHandler(successHandler());
return filter;
}
public static void main(String[] args) {
SpringApplication.run(StackOverflowApplication.class, args);
}
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/**")
.authorizeRequests()
.antMatchers("/login**", "/webjars/**", "/error**")
.permitAll()
.anyRequest()
.authenticated()
;
http.addFilterAfter(oAuth2ClientAuthenticationProcessingFilter(), LogoutFilter.class);
}
}
发布于 2020-01-06 23:04:17
还可以检索预先配置的弹簧过滤器并更改其属性:
@EventListener
public void retrieveAuthenticationFilter(ApplicationStartedEvent event){
FilterChainProxy filterChain = (FilterChainProxy) event.getApplicationContext().getBean(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME);
filterChain.getFilterChains().stream()
.map(f -> f.getFilters())
.flatMap(List::stream)
.filter(f -> f.getClass().isAssignableFrom(OAuth2ClientAuthenticationProcessingFilter.class))
.map(f -> (OAuth2ClientAuthenticationProcessingFilter)f)
.findFirst()
.ifPresent(this::configureAuthenticationFilter);
}
private void configureAuthenticationFilter(OAuth2ClientAuthenticationProcessingFilter authenticationFilter){
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setUseReferer(true);
authenticationFilter.setAuthenticationSuccessHandler(successHandler);
}
https://stackoverflow.com/questions/54958335
复制相似问题