Spring boot KeyClope-角休息集成-角色始终为空

Spring boot KeyClope-角休息集成-角色始终为空,spring-boot,spring-security,keycloak,Spring Boot,Spring Security,Keycloak,当我读到关于将KeyClope与Angular+REST应用程序集成的文章时,我看到的大多数方法都有两个客户机,一个是公共客户机,一个是承载客户机。这是最好的解决方案,还是我可以为两个应用程序使用一个单独的机密客户端。我读到,使用保密的javascript客户端不是最好的方法,因为没有办法将秘密隐藏在javascript中 另外,在使用两个客户机方法将keydape集成到rest和UI项目之后,身份验证似乎起到了作用。但我在其他方面没有任何角色。我使用spring安全适配器和springboot

当我读到关于将KeyClope与Angular+REST应用程序集成的文章时,我看到的大多数方法都有两个客户机,一个是公共客户机,一个是承载客户机。这是最好的解决方案,还是我可以为两个应用程序使用一个单独的机密客户端。我读到,使用保密的javascript客户端不是最好的方法,因为没有办法将秘密隐藏在javascript中

另外,在使用两个客户机方法将keydape集成到rest和UI项目之后,身份验证似乎起到了作用。但我在其他方面没有任何角色。我使用spring安全适配器和springboot 1.5.18作为后端。我的Keyclope服务器版本是3.4.12,Keyclope spring适配器版本是3.4.3。下面还提供了KeyClope配置文件

keydapet.json(角度项目)

为了保护使用注释的rest资源

@RolesAllowed(“Name of the role”)
即使将客户机角色分配给用户,也会抛出403拒绝访问错误

我还尝试使用代码手动获取角色

   SecurityContext securityContext = SecurityContextHolder.getContext();
   securityContext.getAuthentication().getAuthorities();

但它总是返回一个空数组。

我终于解决了这个问题。问题在于前端KeyClope客户端中缺少作用域配置。
由于安全原因,已关闭所有客户端的全部作用域。因此,除非我们在前端客户机的客户机范围配置中明确设置包含后端客户机角色,否则它不会成为令牌的一部分。

我最终能够解决这个问题。问题在于前端KeyClope客户端中缺少作用域配置。 由于安全原因,已关闭所有客户端的全部作用域。因此,除非我们在前端客户机的客户机范围配置中明确设置为包含后端客户机角色,否则它不会成为令牌的一部分

@KeycloakConfiguration

@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)

public class KeycloakSecurityConfig extends KeycloakWebSecurityConfigurerAdapter {


 /**

  * Registers the KeycloakAuthenticationProvider with the authentication manager.

  */

 @Autowired

 public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

  KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();

  keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());

  auth.authenticationProvider(keycloakAuthenticationProvider);

 }



 /**

  * Defines the session authentication strategy.

  */

 @Bean

 @Override

 protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {

  return new NullAuthenticatedSessionStrategy();

 }



 @Bean

 public KeycloakConfigResolver keycloakConfigResolver() {

  return new KeycloakSpringBootConfigResolver();

 }



 @Bean

 public FilterRegistrationBean

 keycloakAuthenticationProcessingFilterRegistrationBean(KeycloakAuthenticationProcessingFilter filter) {

  FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);

  registrationBean.setEnabled(false);

  return registrationBean;

 }


 @Bean

 public FilterRegistrationBean keycloakPreAuthActionsFilterRegistrationBean(KeycloakPreAuthActionsFilter filter) {

  FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);

  registrationBean.setEnabled(false);

  return registrationBean;

 }


 @Bean

 @Scope(scopeName = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)

 public AccessToken accessToken() {

  HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())

   .getRequest();

  return ((KeycloakSecurityContext)((KeycloakAuthenticationToken) request.getUserPrincipal()).getCredentials())

   .getToken();

 }


 @Override

 protected void configure(HttpSecurity http) throws Exception {

  super.configure(http);

  http.authorizeRequests().antMatchers("/**").permitAll();

 }

}
@RolesAllowed(“Name of the role”)
   SecurityContext securityContext = SecurityContextHolder.getContext();
   securityContext.getAuthentication().getAuthorities();