【发布时间】:2018-06-06 22:24:02
【问题描述】:
我使用的是 Spring boot 2.0.0.M7,但无法让 OAuth2 工作。我让它在 M4 下为 facebook 和 github 工作。 (M5改了,当时没试过升级)。
我似乎无法触发从我的应用到 oauth 提供程序的初始重定向。使用提供者 1 (facebook),以前 /login/facebook 过滤器将重定向到外部 OAuth2。 现在...我不知道触发令牌/重定向逻辑需要点击的 URL(可能在文档中丢失?)。
https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-security-oauth2 提到 /login/oauth2/code/* - 但这似乎是针对提供商的响应,并且不会触发重定向。
我当前的配置如下(在 M4 中工作的钩子已作为 cmets 保留)
@Configuration
@EnableWebSecurity
@EnableOAuth2Client
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// ...
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling()
.authenticationEntryPoint(new AuthenticationEntryPoint() {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
response.sendRedirect("/login");
}
})
.and()
.authorizeRequests()
.antMatchers("/", "/favicon.ico", "/static/**", "/login", "/login/**", "/logout", "/at/**").permitAll()
.anyRequest().authenticated()
.and()
// .addFilterBefore(new OAuth2ClientContextFilter(), BasicAuthenticationFilter.class)
// .addFilterBefore(oauthFilter("facebook"), BasicAuthenticationFilter.class)
// .addFilterBefore(githubFilter(), BasicAuthenticationFilter.class)
.csrf()
.disable()
.rememberMe()
.alwaysRemember(true)
;
http.httpBasic().disable();
http.formLogin().disable();
}
// private Filter facebookFilter() {
// @Bean @ConfigurationProperties("facebook.client") public AuthorizationCodeResourceDetails facebook() {
// ...
}
登录页面包含指向 /login/facebook 和 /login/github 的链接。
我的 application.yml 是:
spring.security.oauth2.client.registration:
# developer.facebook.com
facebook:
client-id: redacted
client-secret: redacted
#https://github.com/settings/applications/redacted
github:
client-id: redacted
client-secret: redacted
因为它可能会有所帮助,所以从 build.gradle 中选择了一些依赖项:
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.security.oauth:spring-security-oauth2:2.2.1.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-security'
compile 'org.springframework:spring-context:5.0.2.RELEASE'
compile 'org.springframework.security:spring-security-web:5.0.0.RELEASE'
compile 'org.springframework.security:spring-security-oauth2-client:5.0.0.RELEASE'
我花了很长时间在这个圈子里。欢迎任何帮助:) 谢谢
【问题讨论】:
标签: spring-mvc spring-boot oauth-2.0 spring-security-oauth2