使用 Spring Security OAuth2 验证不同的用户类型
Authenticate different user types with Spring Security OAuth2
我想使用两种类型的用户:普通用户和管理员。现在我已经有了一个基础设施,其中管理员和用户是两种完全不同的类型:用户有很多只与他们相关的东西(控制器、表、服务等),管理员也一样。因此,它们是数据库中不同的实体和不同的表,我不想将它们合并,因为它们是不同的。但现在只有用户可以使用 Spring Security OAuth2 登录,但管理员不能登录,他们无法登录。请注意,我使用自己的授权和资源服务器。
所以,我想允许 Spring 安全性对用户和管理员进行身份验证。我还想为用户和管理员使用两个不同的登录端点和两个不同的实体和表。
如何做到这一点或我应该怎么做?
UPD:
我认为我应该为用户和管理员创建 2 个 OAuth 客户端,在 oauth_client_details
中有 2 个不同的 grant_types
和 2 个 AbstractTokenGranters
。
我已经有一个自定义的 AbstractTokenGranter
用户,可以像这样验证用户:
//getOAuth2Authentication()
User user = userService.getUserByPhone(username);
if(user == null)
throw new BadCredentialsException("Bad credentials");
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(Long.toString(user.getId()), password)
);
//I use Long.toString(user.getId()) because some users use FB instead of the phone,
//so I have one more `AbstractTokenGranter` for social networks,
//I don't mention about it in this post, so don't be confused
据我了解,AuthenticationManager
调用了 UserDetailsService
,现在看起来像这样:
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserDetails user = userRepository.findById(Long.parseLong(username)).orElseThrow(
() -> new UsernameNotFoundException("User not found with id : " + id)
);
return user;
}
但是如果我为管理员再创建一个 AbstractTokenGranter
,那么当前的 UserDetailsService
将不知道它收到了谁的 ID - 管理员 ID 或用户 ID。
作为一种解决方案,我认为我需要为管理员再创建一个 UserDetailsService
。但是我怎样才能使用多个UserDetailsService
呢?另外,也许我应该使用完全不同的方案?
<security:http pattern="/oauth/token" use-expressions="true" create-session="stateless"
authentication-manager-ref="clientAuthenticationManager"
xmlns="http://www.springframework.org/schema/security">
<security:intercept-url pattern="/**" method="GET" access="ROLE_DENY"/>
<security:intercept-url pattern="/**" method="PUT" access="ROLE_DENY"/>
<security:intercept-url pattern="/**" method="DELETE" access="ROLE_DENY"/>
<security:intercept-url pattern="/oauth/token" access="permitAll"/>
<security:anonymous enabled="false"/>
<security:http-basic entry-point-ref="clientAuthenticationEntryPoint"/>
<!-- include this only if you need to authenticate clients via request
parameters -->
<security:custom-filter ref="contentTypeFilter" before="BASIC_AUTH_FILTER"/>
<security:custom-filter ref="clientCredentialsTokenEndpointFilter"
after="BASIC_AUTH_FILTER"/>
<security:access-denied-handler ref="oauthAccessDeniedHandler"/>
<security:csrf disabled="true"/>
</security:http>
您可以定义自定义 clientDetailService 并覆盖 loadUserByUserName 方法。
是否可以查询不同的表和权限取决于您,也可以更改结构。这就是我可以说的,无需更多描述
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
ClientDetails clientDetails;
try {
clientDetails = clientDetailsService.loadClientByClientId(username);
} catch (NoSuchClientException e) {
throw new UsernameNotFoundException(e.getMessage(), e);
}
String clientSecret = clientDetails.getClientSecret();
if (clientSecret== null || clientSecret.trim().length()==0) {
clientSecret = emptyPassword;
}
return new User(username, clientSecret, clientDetails.getAuthorities());
}
可以修改此部分以更改结构:> authentication-manager-ref="clientAuthenticationManager"
如果你没有使用 xml based,你可以检查 annotation base link :
https://www.baeldung.com/spring-security-authentication-with-a-database
1.Create oauth_client_details
table 中的新 OAuth2 客户端和 authorized_grant_types
中的 custom_grant
。
2.Create:
public class CustomTokenGranter extends AbstractTokenGranter {
//...
protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
Map<String, String> params = tokenRequest.getRequestParameters();
String username = params.getOrDefault("username", null);
String password = params.getOrDefault("password", null);
if(username == null || password == null)
throw new BadCredentialsException("Bad credentials");
CustomAuthenticationToken token = new CustomAuthenticationToken(username, password);
Authentication authentication = authenticationManager.authenticate(token);
}
}
3.Add AuthorizationServerConfigurerAdapter
中的授予者:
private TokenGranter tokenGranter(final AuthorizationServerEndpointsConfigurer endpoints) {
List<TokenGranter> granters = new ArrayList<TokenGranter>(Arrays.asList(endpoints.getTokenGranter()));
granters.add(new CustomGrantTokenGranter(endpoints.getTokenServices(), endpoints.getClientDetailsService(), endpoints.getOAuth2RequestFactory(), "custom_grant"));
return new CompositeTokenGranter(granters);
}
现在 CustomGrantTokenGranter
将收到所有授权类型为 custom_grant
的授权请求。
4.Create CustomAuthenticationToken extends UsernamePasswordAuthenticationToken
5.Create:
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Autowired
private PasswordEncoder adminPasswordEncoder;
@Autowired
private UserDetailsService adminDetailsService;
@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
String username = auth.getName();
String password = auth.getCredentials().toString();
UserDetails adminDetails = adminDetailsService.loadUserByUsername(username);
//adminDetailsService.loadUserByUsername(username) returns Admin inside UserDetails
if (adminPasswordEncoder.matches(password, adminDetails.getPassword()))
return new UsernamePasswordAuthenticationToken(adminDetails, password, adminDetails.getAuthorities());
else
throw new BadCredentialsException("Bad credentials");
}
@Override
public boolean supports(Class<?> auth) {
return auth.equals(CustomAuthenticationToken.class);
}
}
在这里您可以使用UserDetailsService
不同于其他供应商
6.Add CustomAuthenticationProvider
在 WebSecurityConfigurerAdapter
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthenticationProvider customAuthenticationProvider;
@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) {
authenticationManagerBuilder.authenticationProvider(customAuthenticationProvider);
}
//...
}
总结: 使用此方案,您可以根据需要使用任意数量的用户类型。如果 Admin implements UserDetails
,那么您可以在服务器上轻松地将其用作 Principal
。
我想使用两种类型的用户:普通用户和管理员。现在我已经有了一个基础设施,其中管理员和用户是两种完全不同的类型:用户有很多只与他们相关的东西(控制器、表、服务等),管理员也一样。因此,它们是数据库中不同的实体和不同的表,我不想将它们合并,因为它们是不同的。但现在只有用户可以使用 Spring Security OAuth2 登录,但管理员不能登录,他们无法登录。请注意,我使用自己的授权和资源服务器。
所以,我想允许 Spring 安全性对用户和管理员进行身份验证。我还想为用户和管理员使用两个不同的登录端点和两个不同的实体和表。
如何做到这一点或我应该怎么做?
UPD:
我认为我应该为用户和管理员创建 2 个 OAuth 客户端,在 oauth_client_details
中有 2 个不同的 grant_types
和 2 个 AbstractTokenGranters
。
我已经有一个自定义的 AbstractTokenGranter
用户,可以像这样验证用户:
//getOAuth2Authentication()
User user = userService.getUserByPhone(username);
if(user == null)
throw new BadCredentialsException("Bad credentials");
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(Long.toString(user.getId()), password)
);
//I use Long.toString(user.getId()) because some users use FB instead of the phone,
//so I have one more `AbstractTokenGranter` for social networks,
//I don't mention about it in this post, so don't be confused
据我了解,AuthenticationManager
调用了 UserDetailsService
,现在看起来像这样:
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserDetails user = userRepository.findById(Long.parseLong(username)).orElseThrow(
() -> new UsernameNotFoundException("User not found with id : " + id)
);
return user;
}
但是如果我为管理员再创建一个 AbstractTokenGranter
,那么当前的 UserDetailsService
将不知道它收到了谁的 ID - 管理员 ID 或用户 ID。
作为一种解决方案,我认为我需要为管理员再创建一个 UserDetailsService
。但是我怎样才能使用多个UserDetailsService
呢?另外,也许我应该使用完全不同的方案?
<security:http pattern="/oauth/token" use-expressions="true" create-session="stateless"
authentication-manager-ref="clientAuthenticationManager"
xmlns="http://www.springframework.org/schema/security">
<security:intercept-url pattern="/**" method="GET" access="ROLE_DENY"/>
<security:intercept-url pattern="/**" method="PUT" access="ROLE_DENY"/>
<security:intercept-url pattern="/**" method="DELETE" access="ROLE_DENY"/>
<security:intercept-url pattern="/oauth/token" access="permitAll"/>
<security:anonymous enabled="false"/>
<security:http-basic entry-point-ref="clientAuthenticationEntryPoint"/>
<!-- include this only if you need to authenticate clients via request
parameters -->
<security:custom-filter ref="contentTypeFilter" before="BASIC_AUTH_FILTER"/>
<security:custom-filter ref="clientCredentialsTokenEndpointFilter"
after="BASIC_AUTH_FILTER"/>
<security:access-denied-handler ref="oauthAccessDeniedHandler"/>
<security:csrf disabled="true"/>
</security:http>
您可以定义自定义 clientDetailService 并覆盖 loadUserByUserName 方法。 是否可以查询不同的表和权限取决于您,也可以更改结构。这就是我可以说的,无需更多描述
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
ClientDetails clientDetails;
try {
clientDetails = clientDetailsService.loadClientByClientId(username);
} catch (NoSuchClientException e) {
throw new UsernameNotFoundException(e.getMessage(), e);
}
String clientSecret = clientDetails.getClientSecret();
if (clientSecret== null || clientSecret.trim().length()==0) {
clientSecret = emptyPassword;
}
return new User(username, clientSecret, clientDetails.getAuthorities());
}
可以修改此部分以更改结构:> authentication-manager-ref="clientAuthenticationManager"
如果你没有使用 xml based,你可以检查 annotation base link : https://www.baeldung.com/spring-security-authentication-with-a-database
1.Create oauth_client_details
table 中的新 OAuth2 客户端和 authorized_grant_types
中的 custom_grant
。
2.Create:
public class CustomTokenGranter extends AbstractTokenGranter {
//...
protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
Map<String, String> params = tokenRequest.getRequestParameters();
String username = params.getOrDefault("username", null);
String password = params.getOrDefault("password", null);
if(username == null || password == null)
throw new BadCredentialsException("Bad credentials");
CustomAuthenticationToken token = new CustomAuthenticationToken(username, password);
Authentication authentication = authenticationManager.authenticate(token);
}
}
3.Add AuthorizationServerConfigurerAdapter
中的授予者:
private TokenGranter tokenGranter(final AuthorizationServerEndpointsConfigurer endpoints) {
List<TokenGranter> granters = new ArrayList<TokenGranter>(Arrays.asList(endpoints.getTokenGranter()));
granters.add(new CustomGrantTokenGranter(endpoints.getTokenServices(), endpoints.getClientDetailsService(), endpoints.getOAuth2RequestFactory(), "custom_grant"));
return new CompositeTokenGranter(granters);
}
现在 CustomGrantTokenGranter
将收到所有授权类型为 custom_grant
的授权请求。
4.Create CustomAuthenticationToken extends UsernamePasswordAuthenticationToken
5.Create:
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Autowired
private PasswordEncoder adminPasswordEncoder;
@Autowired
private UserDetailsService adminDetailsService;
@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
String username = auth.getName();
String password = auth.getCredentials().toString();
UserDetails adminDetails = adminDetailsService.loadUserByUsername(username);
//adminDetailsService.loadUserByUsername(username) returns Admin inside UserDetails
if (adminPasswordEncoder.matches(password, adminDetails.getPassword()))
return new UsernamePasswordAuthenticationToken(adminDetails, password, adminDetails.getAuthorities());
else
throw new BadCredentialsException("Bad credentials");
}
@Override
public boolean supports(Class<?> auth) {
return auth.equals(CustomAuthenticationToken.class);
}
}
在这里您可以使用UserDetailsService
不同于其他供应商
6.Add CustomAuthenticationProvider
在 WebSecurityConfigurerAdapter
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthenticationProvider customAuthenticationProvider;
@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) {
authenticationManagerBuilder.authenticationProvider(customAuthenticationProvider);
}
//...
}
总结: 使用此方案,您可以根据需要使用任意数量的用户类型。如果 Admin implements UserDetails
,那么您可以在服务器上轻松地将其用作 Principal
。