Spring security Spring引导安全配置OAuthso和ResourceServer

Spring security Spring引导安全配置OAuthso和ResourceServer,spring-security,oauth-2.0,single-sign-on,Spring Security,Oauth 2.0,Single Sign On,我有一个由两部分组成的WebApp。 一个是前端(Vaadin),我希望用户通过OAuth2登录。然后检查用户是否具有特定角色。-->如果用户打开URL,他将自动重定向到OAuthLogin。-->这是与@enableAuthSSO一起工作的 第二部分是应用程序的REST-API,它可以在/API/*下找到。fE/api/设备 如果Get请求具有有效的承载令牌,应该给我一个列表。如果GET请求没有承载令牌,或者如果想要获取403,则角色(权限)错误 下面是我的配置: @Configuration

我有一个由两部分组成的WebApp。 一个是前端(Vaadin),我希望用户通过OAuth2登录。然后检查用户是否具有特定角色。-->如果用户打开URL,他将自动重定向到OAuthLogin。-->这是与@enableAuthSSO一起工作的

第二部分是应用程序的REST-API,它可以在/API/*下找到。fE/api/设备 如果Get请求具有有效的承载令牌,应该给我一个列表。如果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/devices时,我也被迫通过Oauth登录。即使我发送了带有身份验证的Http头:承载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\u API\u Role”。

我对SSO有同样的问题,我为此配置了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
    }
}
我没有在我的项目中配置WebSecurityConfigureAdapter

选中此项:


您好,我对SSO有同样的问题,我为此配置了ResourceService: