首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Boot安全配置OAuthSSO和ResourceServer

Spring Boot安全配置OAuthSSO和ResourceServer
EN

Stack Overflow用户
提问于 2020-01-09 21:37:27
回答 1查看 110关注 0票数 0

我有一个由两部分组成的WebApp。一个是与前端(Vaadin),在那里我希望用户登录通过OAuth2。然后,我检查用户是否具有某个角色。-->如果用户打开URL,会自动跳转到OAuthLogin。-->这是使用@EnableOAuthSso。

第二部分是应用程序的REST-API,可以通过/api/*下的任何内容找到它。fE。如果Get-Request有一个有效的承载令牌,/api/device应该会给我一个列表。如果GET请求没有承载令牌,或者如果想要获得403,则返回错误的角色(授权)。

下面是我的配置:

代码语言:javascript
复制
@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定义了以下行:

代码语言:javascript
复制
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“角色。

EN

回答 1

Stack Overflow用户

发布于 2020-06-17 20:57:00

我对单点登录也有同样的问题,我为此配置了一个ResourceServe:

代码语言:javascript
复制
@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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59665215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档