【发布时间】:2022-07-12 14:31:01
【问题描述】:
我正在使用 postman 和 spring boot,我使用 Keycloak 进行身份验证服务。我通过邮递员向 Keycloak 服务器发出请求,该服务器返回给我一个 Bearear 令牌,然后我将其发送到 Spring 服务器进行身份验证,但 Spring 回复说令牌的 iss 声明无效。
这是我的代码
类配置:
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers("/genere/**").permitAll()
.antMatchers("/valutazione/**").permitAll()
.antMatchers("/users/**").permitAll()
.antMatchers("/carrello/**").permitAll()
.antMatchers("/film/**").permitAll()
.anyRequest().authenticated().and().oauth2ResourceServer().jwt();
}
类休息控制器:
@RestController
public class HomeController {
@GetMapping("/")
@PreAuthorize("hasAuthority('user')")
public String home(@RequestParam(value="someValue") int value){
return "Welcome,"+ Util.getEmail()+" "+value+" !";
}
}
应用程序.yaml
keycloak:
realm: demo
resource: spa-demo
auth-server-url: http://localhost:8080/realms/demo/account
spring:
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri: http://localhost:8080/realms/demo/protocol/openid-connect/certs
issuer-uri: http://localhost:8080/realms/demo/
我在开发模式下使用命令 kc.bat start-dev 启动 keyclaok。
【问题讨论】:
标签: spring spring-boot authentication jwt keycloak