使用oauth2进行spring sso身份验证如何工作?

时间:2019-05-22 11:06:36

标签: spring-security spring-security-oauth2

我已经设置了两个在不同端口和授权服务器上运行的oauth2客户端Spring Boot 2 Web应用程序。如果我在一个Web应用程序上进行身份验证,则可以在另一个Web应用程序上访问安全资源。辉煌,行动中的SSO!

但是,从网络流量来看,我看不到标头上的任何Bearer令牌,只有一些与SESSIONID相关的cookie。 HTTP会话的使用使我担心水平扩展可能是一个问题。然后我意识到第二个应用程序正在工作并且以某种方式进行身份验证。

那么,浏览器将调用传递给第二个Web应用程序以使其能够使用现有身份验证了吗?春季安全性使用http会话是否会导致扩展方面的问题。

谢谢

ui oauth2客户端application.yml(使用Spring Boot 2安全性的oauth2客户端实现)

spring:
  profiles: oauth2-security
  security:
    oauth2:
      client:
        registration:
          myoauth:
            client-id: myoauth-trusted-client
            client-secret: ...
            authorization-grant-type: authorization_code
            redirect-uri-template: http://localhost:${server.port}/ui/login/oauth2/code/myoauth
        provider:
          myoauth:
            authorization-uri: http://localhost:8081/auth/oauth/authorize
            token-uri: http://localhost:8081/auth/oauth/token
            user-info-uri: http://localhost:8081/auth/user_info
            user-info-authentication-method: header
            user-name-attribute: name

authz服务器。使用垫片罐spring-security-oauth2-autoconfigure

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    private AuthenticationManager authenticationManager;

    public AuthorizationServerConfig(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        this.authenticationManager = authenticationConfiguration.getAuthenticationManager();
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) {
        security.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
        endpoints
                .authenticationManager(this.authenticationManager) //for use with password grant type
                .authorizationCodeServices(new InMemoryAuthorizationCodeServices()); //for use with authorization_code grant type
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("myoauth-trusted-client")
                    .authorizedGrantTypes("authorization_code")
                    .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
                    .secret("{bcrypt}" + new BCryptPasswordEncoder().encode("..."))
                    .scopes("all")
                    .autoApprove(true)
                    .redirectUris("http://localhost:8082/ui/login/oauth2/code/myoauth", "http://localhost:8083/ui/login/oauth2/code/myoauth").and()
                .withClient("myoauth-client-with-secret")
                    .authorizedGrantTypes("password", "client_credentials")
                    .authorities("ROLE_CLIENT")
                    .scopes("read")
                    .secret("{bcrypt}" + new BCryptPasswordEncoder().encode("..."))
        ;
    }
}

0 个答案:

没有答案