如何实现自定义身份验证?该问题涉及IT技术。

7

我需要将我的系统与第三方供应商集成。这个系统是由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吗?


您可以在以下链接中找到使用Spring OAuth2和Angular的示例: http://www.baeldung.com/rest-api-spring-oauth2-angularjs - Samir
我强烈推荐这个示例/教程 -> 使用Spring Boot进行JWT身份验证 http://www.svlada.com/jwt-token-authentication-with-spring-boot/#jwt-authentication - Harsha Jayamanna
3个回答

1
你可以使用Spring Security。流程如下:您对安全令牌服务进行身份验证。包含认证令牌的cookie将写入您的浏览器。在每个后续请求中,该令牌都将发送到服务器。
在rest服务器上,您将使用Srping Security,更具体地说,您需要在其实现中使用AbstractPreAuthenticatedProcessingFilter,以提取令牌并将其与安全上下文关联。
这是您的Spring Security示例配置。
@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;
    }





}

登录/注销的端点在哪里?我应该把收到的令牌存储在哪里? - Murillo Goulart
令牌将由Spring Security自动存储在Subject Handler中。这是通过抽象类PreAuthenticatedAuthenticationToken完成的。登录和注销是令牌提供程序的一部分。例如,如果您使用OpenAM,则OpenAM本身带有登录和注销功能。 - Alexander Petrov

0

是的,您可以使用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();
}

现在您可以使用以下内容-

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

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


登录/注销终端在哪里?我应该把收到的令牌存储在哪里? - Murillo Goulart
登录终点 - 令牌终点用于获取访问令牌,如果令牌有效,则将用户视为已登录用户。这种流程不受OpenID连接ID的支持。 - ManishSingh
存储令牌 - 如果您正在使用Angular,则可以将其存储在浏览器本地存储中。 - ManishSingh
登出端点 - 此流程中没有登出端点,只需从本地存储中删除令牌。 - ManishSingh
请告诉我如果您有任何其他疑问。您可以通过此链接获取更多详细信息 - https://www.linkedin.com/pulse/microservices-security-openid-connect-manish-singh - ManishSingh

0

查看这里的示例

使用JWT与Angular的Spring Security OAuth2

在本教程中,我们将讨论如何使我们的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;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接