我必须将我的系统与第三方提供商集成。这个系统是用 Spring 和 Angular 制作的。
请记住,我需要创建一个自定义登录表单,而不是重定向到 OAuth2 等第三方提供商表单。
他创建了以下端点:
获取 token 认证
POST http://example.com/webapi/api/web/token
“username=972.344.780-00&password=123456&grant_type=password”
响应向我发送了一个 token ,我必须在接下来的所有请求中使用它。
获取用户信息
Authorization: Bearer V4SQRUucwbtxbt4lP2Ot_LpkpBUUAl5guvxAHXh7oJpyTCGcXVTT-yKbPrPDU9QII43RWt6zKcF5m0HAUSLSlrcyzOuJE7Bjgk48enIoawef5IyGhM_PUkMVmmdMg_1IdIb3Glipx88yZn3AWaneoWPIYI1yqZ9fYaxA-_QGP17Q-H2NZWCn2lfF57aHz8evrRXNt_tpOj_nPwwF5r86crEFoDTewmYhVREMQQjxo80
GET http://example.com/webapi/api/web/userInfo
也就是说,我需要什么来实现自定义身份验证?
我可以在这种情况下使用 Spring OAuth2 吗?
最佳答案
您可以使用 Spring Security。流程如下。您根据安全 token 服务进行身份验证。包含身份验证 token 的 cookie 将写入您的浏览器。此 token 在针对服务器的每个后续请求中发送。
在其余服务器上,您将使用 Srping Security,更具体地说,您需要在其实现中使用 AbstractPreAuthenticatedProcessingFilter,您将提取 token 并将其与安全上下文相关联。
这是您的 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;
}
}
关于java - 如何实现自定义身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46976928/