我有一个由两部分组成的WebApp。一个是与前端(Vaadin),在那里我希望用户登录通过OAuth2。然后,我检查用户是否具有某个角色。-->如果用户打开URL,会自动跳转到OAuthLogin。-->这是使用@EnableOAuthSso。
第二部分是应用程序的REST-API,可以通过/api/*下的任何内容找到它。fE。如果Get-Request有一个有效的承载令牌,/api/device应该会给我一个列表。如果GET请求没有承载令牌,或者如果想要获得403,则返回错误的角色(授权)。
下面是我的配置:
@Configuration
@EnableOAuth2Sso
public class ProdWebSecurityConfiguration extends WebSecurityConfigurerAdapter {
private static final String ADMIN_ROLE= "role.global.admin";
private static final String READ_API_ROLE= "role.base.read.api";
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/login**", "/error**").permitAll()
.antMatchers("/*").hasAuthority(ADMIN_ROLE)
.antMatchers("/api/**").hasAnyAuthority(ADMIN_ROLE, READ_API_ROLE)
.and().logout().permitAll().logoutSuccessUrl(rootAuthUri + "/connect/endsession")
;
}现在,例如,当我在浏览器中打开/manageDevices时,我被强制通过身份验证码流程登录,一切都像预期的那样工作。
当我尝试打开/api/device时,我也被强制通过Oauth登录。即使我发送的Http-Header带有身份验证:持有者xxxxx。不知何故,它总是迫使我从我的OAuth登录到登录屏幕。
application.properties定义了以下行:
base.rootauthuri=https://oauth2.mypage.ch
security.oauth2.client.clientId=client.base.parameters
security.oauth2.client.clientSecret=secret
security.oauth2.client.accessTokenUri=${base.rootauthuri}/connect/token
security.oauth2.client.userAuthorizationUri=${base.rootauthuri}/connect/authorize
security.oauth2.client.scope=openid,scope.base.parameters,role,offline_access
security.oauth2.client.clientAuthenticationScheme=form
security.oauth2.resource.userInfoUri=${base.rootauthuri}/connect/userinfo如何才能强制/api/*下的所有内容不重定向到AuthenticationForm,但如果没有发送承载令牌,则返回403。如何检查承载令牌是否也具有"READ_API_ROLE“角色。
发布于 2020-06-17 20:57:00
我对单点登录也有同样的问题,我为此配置了一个ResourceServe:
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Autowired
private ResourceServerConfiguration configuration;
@PostConstruct
public void setSecurityConfigurerOrder() {
configuration.setOrder(3);
}
@Bean("resourceServerRequestMatcher")
public RequestMatcher resources() {
return new AntPathRequestMatcher("/api/**");
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.requestMatchers().antMatchers("/v1/**") // this is free resource
.and().authorizeRequests()
.antMatchers("/api/**").permitAll() // This is free resource for mvc calls
// Usado para paths que necessitam de token bearer
.and().requestMatchers().antMatchers("/integration/**")
.and().authorizeRequests()
.antMatchers("/integration/**").authenticated(); // this is protected resource, it's necessary token
}
}我没有在项目中配置WebSecurityConfigurerAdapter;
请检查以下内容:
Spring Boot 1.3.3 @EnableResourceServer and @EnableOAuth2Sso at the same time
https://www.baeldung.com/spring-security-oauth2-enable-resource-server-vs-enable-oauth2-sso
https://stackoverflow.com/questions/59665215
复制相似问题