带Spring boot的Key斗篷-如何应用资源范围
我在Spring Boot 1.5.16中使用Key斗篷7.0.1。通过指定资源、基于角色的策略和权限来释放保护端点的安全性,这是可以预期的 棘手的事情是只保护对某个特定URI的POST和GET请求。我在application.yml中做了什么:带Spring boot的Key斗篷-如何应用资源范围,spring,spring-boot,keycloak,Spring,Spring Boot,Keycloak,我在Spring Boot 1.5.16中使用Key斗篷7.0.1。通过指定资源、基于角色的策略和权限来释放保护端点的安全性,这是可以预期的 棘手的事情是只保护对某个特定URI的POST和GET请求。我在application.yml中做了什么: policy-enforcer-config: enforcement-mode: ENFORCING paths[0]: name: all path: /* paths[1]: name: test post
policy-enforcer-config:
enforcement-mode: ENFORCING
paths[0]:
name: all
path: /*
paths[1]:
name: test post
path: /my/url
methods[0]:
method: GET
scopes[0]: view
methods[1]:
method: POST
scopes[0]: edit
在keyclaok中,我创建了edit
和view
作用域,/my/url
资源、具有角色和否定决策的策略(如果用户具有该角色-拒绝访问),权限包含资源、作用域和策略。评估工作正常,但我的spring应用程序总是收到403错误
你能给我一个资源范围使用的例子吗?或者你能给我一些建议吗 可能有多个问题,但首先,请检查您的SecurityConfig 这就是我们所拥有的:
@Configuration
@EnableWebSecurity
@KeycloakConfiguration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableConfigurationProperties(KeycloakSpringBootProperties.class)
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {
bla-bla..
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers(HttpMethod.POST, "/auth/**")
.antMatchers(HttpMethod.OPTIONS,"/**")
// allow anonymous resource requests
.and()
.ignoring()
.antMatchers(
HttpMethod.GET,
"/",
"/*.html",
"/favicon.ico",
"/**/*.html",
"/**/*.css",
"/**/*.js",
"/actuator/**"
)
;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// we don't need CSRF because our token is invulnerable
.csrf().disable()
.exceptionHandling()
.defaultAuthenticationEntryPointFor(
getRestAuthenticationEntryPoint(),
new AntPathRequestMatcher("/**")
)
.authenticationEntryPoint(unauthorizedHandler).and()
// don't create session
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
// system state endpoint
.antMatchers("/ping").permitAll()
.antMatchers(HttpMethod.GET, "/whatever/you/need/to/open/to/public/one", "/whatever/you/need/to/open/to/public/two").permitAll()
// User authentication actions
.antMatchers("/auth/**").permitAll()
.antMatchers("/**/*.css").permitAll()
.anyRequest().authenticated()
;
http
.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
;
// disable page caching
http
.headers()
.frameOptions().sameOrigin()
.cacheControl();
}
如果您想用role限制RESTAPI端点,请为控制器方法添加@PreAuthorize(“hasRole('your.role.from.keydape')”)Thx,这已经实现并起作用了。无法仅利用POST和GET隔离
@EnableGlobalMethodSecurity(prespenabled=true)
您是否将此注释与spring security一起使用?不,仅使用ant matchers