为什么不推荐使用@EnableOAuth2Sso?

MrY*_*Yui 5 java spring-security oauth-2.0

为什么@EnableOAuth2Sso在 Spring Security 中被弃用?这是 OAuth2 对我有用的唯一原因。

如果我删除@EnableOAuth2Sso,那么这将不起作用

@Configuration
@EnableOAuth2Client
@EnableOAuth2Sso <- Need to have this!
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable()
        .authorizeRequests()
        .antMatchers("/Intranet/Bokning").authenticated()
        .antMatchers("/**", "/Intranet**").permitAll()
        .anyRequest().authenticated()
        .and().logout().logoutSuccessUrl("/").permitAll();
    }

}
Run Code Online (Sandbox Code Playgroud)

还有其他解决方案吗?

MrY*_*Yui 5

这是最新的 Spring Security 与 Facebook OAuth2.0 的解决方案。

安全:

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {

        http
        .csrf().disable()
        .authorizeRequests()
        .antMatchers("/Intranet/Bokning").authenticated() // Block this 
        .antMatchers("/**", "/Intranet**").permitAll() // Allow this for all
        .anyRequest().authenticated()
        .and().logout().logoutSuccessUrl("/").permitAll()
        .and()
        .oauth2Login();
    }
}
Run Code Online (Sandbox Code Playgroud)

appllication.yml

spring:
  security:
    oauth2:
      client:
        registration:
           facebook:
              clientId: myID
              clientSecret: mySecret
              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

server:
  port: 8080
Run Code Online (Sandbox Code Playgroud)

pom.xml文件:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-oauth2-client</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

  • WebSecurityConfigurerAdapter 类现已弃用。 (4认同)