Java 在控制器方法调用之前,将OAuth2用户属性映射到对象 我目前拥有的
我目前正在为我的公司实施一个OIDC资源提供商。他们使用InternoidC服务器,我通过以下示例成功地使用了它: 我现在可以从授权服务器检索用户信息,如:Java 在控制器方法调用之前,将OAuth2用户属性映射到对象 我目前拥有的,java,spring,spring-security,oauth-2.0,spring-security-oauth2,Java,Spring,Spring Security,Oauth 2.0,Spring Security Oauth2,我目前正在为我的公司实施一个OIDC资源提供商。他们使用InternoidC服务器,我通过以下示例成功地使用了它: 我现在可以从授权服务器检索用户信息,如: @RestController @请求映射(“/some/route”) 公共类控制器{ @GetMapping(“/some/route”) 公共响应权限获取内容(@RegisteredAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient){ 字符串userInfoEn
@RestController
@请求映射(“/some/route”)
公共类控制器{
@GetMapping(“/some/route”)
公共响应权限获取内容(@RegisteredAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient){
字符串userInfoEndpointUri=authorizedClient.getClientRegistration()
.getProviderDetails().getUserInfoEndpoint().getUri();
Map userAttributes=this.webClient
.get()
.uri(userInfoEndpointUri)
.attributes(oauth2AuthorizedClient(authorizedClient))
.retrieve()
.bodyToMono(地图类)
.block();
String firstName=(String)userAttributes.get(“first_name”);
String lastName=(String)userAttributes.get(“last_name”);
...
}
}
我想要什么
我现在正在寻找一种解决方案,将userAttributes
映射到
控制器方法调用,以便我获得例如:
@GetMapping(“/some/route”)
公共响应获取某物(mycostumeserbean用户){
字符串firstName=user.getFirstName();
字符串lastName=user.getLastName();
...
}
我读了一些关于频道拦截器
和手持拦截器
以及PrincipalExtractor
和权威提取器
的资料。
问题是,我只是在学习Spring框架,这些可能性让我无法承受
如果该方法允许一些验证,并且在验证失败时会立即响应错误代码,那将是一个加号
完成后,我想从另一台服务器向MyCastumuserBean添加额外信息,我将当前会话用户的身份发送给该服务器,并接收该用户的角色/权限等信息
我试着把它放在一张照片里:
问题:
我想您是在询问如何配置成功处理程序,或者是一个可以检查用户属性的过滤器 如果这就是你要问的,有很多方法可以做到。 例如: 使用用户范围检查:(需要提前将范围分配给用户。) 如果您想为您的安全配置应用过滤器:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
...
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
...
.and()
.addFilterBefore(getBeforeAuthenticationFilter(), CustomBeforeAuthenticationFilter.class)
.formLogin()
.loginPage()
.permitAll()
...
}
public UsernamePasswordAuthenticationFilter getBeforeAuthenticationFilter() throws Exception {
CustomBeforeAuthenticationFilter filter = new CustomBeforeAuthenticationFilter();
....
return filter;
}
}
您还可以通过使用自定义过滤器链,通过在其中指定不同的顺序和相对登录名,来实现相同的目的
@Configuration
@Order(SecurityProperties.BASIC_AUTH_ORDER - 10)
public class ApplicationConfigurerAdapterForUserGroup1 extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
...;
}
}
@Configuration
@Order(SecurityProperties.BASIC_AUTH_ORDER - 10)
public class ApplicationConfigurerAdapterForUserGroup2 extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
...;
}
}
谢谢,但这并不能真正回答我的问题。或者你可能只想使用:@PreAuthorize(“”)或#@PostAuthorize(“returnObject.get”(“first_name”)=“testuser”)
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
...
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
...
.and()
.addFilterBefore(getBeforeAuthenticationFilter(), CustomBeforeAuthenticationFilter.class)
.formLogin()
.loginPage()
.permitAll()
...
}
public UsernamePasswordAuthenticationFilter getBeforeAuthenticationFilter() throws Exception {
CustomBeforeAuthenticationFilter filter = new CustomBeforeAuthenticationFilter();
....
return filter;
}
}
@Configuration
@Order(SecurityProperties.BASIC_AUTH_ORDER - 10)
public class ApplicationConfigurerAdapterForUserGroup1 extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
...;
}
}
@Configuration
@Order(SecurityProperties.BASIC_AUTH_ORDER - 10)
public class ApplicationConfigurerAdapterForUserGroup2 extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
...;
}
}