【问题标题】:How I can implement a custom authentication?如何实现自定义身份验证?
【发布时间】:2018-04-09 04:05:41
【问题描述】:

我必须将我的系统与第三方提供商集成。该系统由 Spring 和 Angular 组成。

请记住,我需要创建一个自定义登录表单,而不是重定向到像 OAuth2 这样的第三方提供商表单。

他创建了以下端点:

获取令牌认证

POST http://example.com/webapi/api/web/token

“username=972.344.780-00&password=123456&grant_type=password”

响应向我发送了一个令牌,我必须在所有下一个请求期间使用该令牌。

获取用户信息

Authorization: Bearer V4SQRUucwbtxbt4lP2Ot_LpkpBUUAl5guvxAHXh7oJpyTCGcXVTT-yKbPrPDU9QII43RWt6zKcF5m0HAUSLSlrcyzOuJE7Bjgk48enIoawef5IyGhM_PUkMVmmdMg_1IdIb3Glipx88yZn3AWaneoWPIYI1yqZ9fYaxA-_QGP17Q-H2NZWCn2lfF57aHz8evrRXNt_tpOj_nPwwF5r86crEFoDTewmYhVREMQQjxo80

GET http://example.com/webapi/api/web/userInfo

也就是说,我需要什么来实现自定义身份验证?

在这种情况下我可以使用 Spring OAuth2 吗?

【问题讨论】:

标签: java angularjs spring spring-security spring-oauth2


【解决方案1】:

看这里的例子

将 JWT 与 Spring Security OAuth2 与 Angular 结合使用

在本教程中,我们将讨论如何让我们的 Spring Security OAuth2 实现使用 JSON Web Tokens。

http://www.baeldung.com/spring-security-oauth-jwt

@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(tokenStore())
                 .accessTokenConverter(accessTokenConverter())
                 .authenticationManager(authenticationManager);
    }

    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(accessTokenConverter());
    }

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey("123");
        return converter;
    }

    @Bean
    @Primary
    public DefaultTokenServices tokenServices() {
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore(tokenStore());
        defaultTokenServices.setSupportRefreshToken(true);
        return defaultTokenServices;
    }
}

【讨论】:

    【解决方案2】:

    您可以使用 Spring Security。流程如下。您根据安全令牌服务进行身份验证。包含身份验证令牌的 cookie 将写入您的浏览器。在针对服务器的每个后续请求中都会发送此令牌。

    在其余服务器上,您将使用 Srping Security,更具体地说,您需要在其实现中使用 AbstractPreAuthenticatedProcessingFilter,您将提取令牌并将其与安全上下文相关联。

    这是您的 spring 安全性的示例配置

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    
      @Bean
      public AuthenticationManager authenticationManagerBean() throws Exception {
        // TODO Auto-generated method stub
        return super.authenticationManagerBean();
      }
    
      public void configure(WebSecurity web) throws Exception {
            // do some configuration here
      }
    
      @Override
      public void configure(HttpSecurity http) throws Exception {
           // configure your Security here 
           // you can add your implementation of AbstractPreAuthenticatedProcessingFilter here
      }
    
    }
    

    这是您的附加配置

    @Configuration
    public class ExampleSpringSecurityConfig{
    
    
        @Bean
        public AuthenticationManager authenticationManager() {
            return authentication -> authProvider().authenticate(authentication);
        }
    
        private AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> userdetailsService() {
           // Construct your AuthenticationUserDetailsService here
       }
    
        @Bean
        public PreAuthenticatedAuthenticationProvider authProvider() {
            PreAuthenticatedAuthenticationProvider authProvider = new PreAuthenticatedAuthenticationProvider();
            authProvider.setPreAuthenticatedUserDetailsService(userdetailsService());
            return authProvider;
        }
    
    
    
    
    
    }
    

    【讨论】:

    • 登录/注销端点在哪里?我将收到的令牌存储在哪里?
    • 令牌将由 spring security 自动存储在 Subject Handler 中。这是由抽象类 PreAuthenticatedAuthenticationToken 完成的。登录注销是令牌提供程序的一部分。例如,如果您使用 OpenAM,则 OpenAM 本身带有 Login 和 Logout。
    【解决方案3】:

    是的,您可以使用 Spring Oauth2。您必须实施资源所有者密码凭据授予 Oauth2 流程。 您必须为最终用户创建一个登录页面,并且您的客户端应用程序会将用户的凭据以及您的客户端系统凭据(对客户端系统凭据使用 HTTP 基本身份验证)发送到授权服务器以获取令牌。

    有两种实现方式-

    1. 使用客户端系统 ID 和密码 - 使用此授权类型调用令牌端点时,您需要传入客户端 ID 和密码(使用基本身份验证)。

    curl -u 972.344.780-00:123456 "http://example.com/webapi/api/web/token?grant_type=password&username=addEndUserNameHere&password=addEndUserPasswordHere"

    • 仅使用客户端系统 ID(无客户端系统密码)- 授权服务器应具有客户端设置以支持此流程而无需任何密码-

    AuthorizationServerConfigurerAdapter 的子类应该有以下代码-

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {    
            clients.inMemory()
                .withClient("clientId")
                .authorizedGrantTypes("password")
                .authorities("ROLE_CLIENT")
                .scopes("read");
        }
     }
    
    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.allowFormAuthenticationForClients();
    }
    

    现在你可以在下面使用-

    发布http://example.com/webapi/api/web/token?grant_type=password&client_id=my-trusted-client&scope=trust&username=addEndUserNameHere&password=addEndUserPasswordHere

    注意 - 此流程的安全性低于其他 Oauth2 流程,建议仅用于受信任的客户端应用程序,因为用户必须向客户端应用程序提供凭据。

    【讨论】:

    • 登录/注销端点在哪里?我将收到的令牌存储在哪里?
    • 登录端点 - 令牌端点用于获取访问令牌,如果令牌有效,则将用户视为登录用户。 OpenID 连接 ID 不支持此流程。
    • 存储令牌 - 假设您使用 Angular,您可以将其存储在浏览器本地存储中。
    • 注销端点 - 此流程没有注销端点,只是从本地存储中删除了令牌。
    • 如果您还有任何疑问,请告诉我。您可以通过此链接获取更多详细信息 - linkedin.com/pulse/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 2022-12-10
    相关资源
    最近更新 更多