当前位置: 首页 > 知识库问答 >
问题:

Spring安全钥匙斗篷适配器

公西马鲁
2023-03-14

我正试图在我的spring boot应用程序中使用KeyClope。

我想根据REST方法和用户角色限制对特定URL的访问。

在下面的示例中,具有查看所有日历角色的用户可以执行GET,而具有管理所有日历_管理角色的用户可以执行POST、PUT或DELETE。

不幸的是,此配置允许任何经过身份验证的用户访问/api/日历URL。我做错了什么?

@Configuration
@EnableWebSecurity
class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
...
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        super.configure(http);
        http
                .csrf().disable()
                .antMatcher("/api/**")
                .authorizeRequests()
                .antMatchers(HttpMethod.GET, "/api/calendar/*").hasAnyRole("view-all", "calendar")
                .antMatchers(HttpMethod.POST, "/api/calendar/*").hasAnyRole("manage-all", "calendar_manage")
                .antMatchers(HttpMethod.PUT, "/api/calendar/*").hasAnyRole("manage-all", "calendar_manage")
                .antMatchers(HttpMethod.DELETE, "/api/calendar/*").hasAnyRole("manage-all", "calendar_manage");
    }
}

共有2个答案

通煜祺
2023-03-14

诀窍是——在ant matchers中的路径末尾需要一个/**

(我必须一步一步地遍历spring代码才能找到答案——请参阅spring Core的AntPathMatcher类中的方法doMatch()

.antMatchers(HttpMethod.GET, "/calendar/**")
    .hasAnyRole("view-all", "calendar", "calendar_manage")
.antMatchers(HttpMethod.POST, "/calendar/**")
    .hasAnyRole("manage-all", "calendar_manage")
.antMatchers(HttpMethod.PUT, "/calendar/**")
     .hasAnyRole("manage-all", "calendar_manage")
.antMatchers(HttpMethod.DELETE, "/calendar/**")
    .hasAnyRole("manage-all", "calendar_manage")
章晋鹏
2023-03-14

请尝试使用isAuthenticated,并拥有任何权限(“/api/**”)和权限

       @Configuration
        @EnableWebSecurity
        class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
        ...
            @Override
            protected void configure(HttpSecurity http) throws Exception
            {
                super.configure(http);
                http    .authorizeRequests()
                        .csrf().disable()
                        .antMatcher("/api/**").permitAll()
                        .isAuthenticated()
                        .antMatchers(HttpMethod.GET, "/api/calendar/*").hasAnyAuthority("view-all", "calendar")
                        .antMatchers(HttpMethod.POST, "/api/calendar/*").hasAnyAuthority("manage-all", "calendar_manage")
                        .antMatchers(HttpMethod.PUT, "/api/calendar/*").hasAnyAuthority("manage-all", "calendar_manage")
                        .antMatchers(HttpMethod.DELETE, "/api/calendar/*").hasAnyAuthority("manage-all", "calendar_manage");
            }
        }
 类似资料:
  • 我正在开发一个连接到java(Spring框架)后端的角webapp。身份验证是通过密钥斗篷服务器完成的。 在我的带有嵌入式tomcat服务器的本地机器上,angular应用程序和spring应用程序运行正常。 对于部署,我需要使用传统的方式,使用现有的tomcat服务器。 角度前端在根目录中通过http://myurl/spring后端作为war文件放置,可以通过http://myurl/api

  • 我用这个例子:https://github.com/foo4u/keycloak-spring-demo 我有钥匙斗篷。json文件在我的WEB-INF文件夹中,但当我运行应用程序时,会出现以下异常: 此异常的完整堆栈: 这段代码只是Spring Boot和KeyCloak集成的一个例子。 你能告诉我我做错了什么吗? 谢谢你的回答和时间。

  • 我有一个问题运行Spring启动和钥匙斗篷都在docker容器。 我从在docker中运行的数据库mysql的KeyClope开始。 然后我添加了我的领域(SpringBootKey斗篷)、我的客户机(testclient)和一个角色为“user”的用户。之后,我将Spring Security性添加到我的Spring Boot应用程序中。编辑了我的申请。yml 根据我添加了我的SecurityC

  • 我有java REST应用程序,我希望通过keycloak保护。我做了一些测试,应用程序与keycloak servlet过滤器配合良好,但我在Spring Security适配器方面遇到了问题。 钥匙斗篷。json(删除值) 可见,我只需要承载授权,若请求包含有效令牌,那个么若不是http 401,那个么我希望是http 200。我使用了以下xml配置: 到底发生了什么?请求经过身份验证,但成功

  • 我正试图实现一个带有Spring Boot Restservice的Angular应用程序,该服务由KeyClope保护。 在我的电脑上,一切正常。Angular应用程序是由KeyClope引导的(使用KeyClope Angular),因此我必须登录才能查看该应用程序。应用程序将REST调用与令牌一起发送到Spring Boot。REST服务是KeyClope。承载者仅接收令牌,并使用令牌中的角