![](/img/trans.png)
[英]405 Method Not Allowed, spring boot, but i used csrf token header
[英]Cannot make a request with Access Token from Keycloak in Spring Boot Microservices (Rest Template Issue 405 Method Not Allowed)
我在使用 Spring 引导微服务中的 docker 中运行的 Keycloak 发出访问令牌请求时遇到问题。
在用户服务中创建用户并登录后,我遇到了管理服务问题。
当我尝试使用从 Keycloak 定义访问令牌向此 url (http://localhost:9002/api/v1/user_role/alladvertisements) 发出请求时,我收到了 405 不允许的问题。
我该如何解决?
这是我的SecurityConfig,如下所示。
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(jsr250Enabled = true)
public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/api/v1/admin_role").hasAnyRole("ROLE_ADMIN")
.antMatchers("/api/v1/user_role").hasAnyRole("ROLE_USER")
.antMatchers("/actuator/health").hasAnyRole("ROLE_ADMIN")
.antMatchers("/actuator/circuitbreakerevents").hasAnyRole("ROLE_ADMIN")
.anyRequest()
.permitAll();
}
@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
KeycloakAuthenticationProvider provider = keycloakAuthenticationProvider();
provider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
auth.authenticationProvider(provider);
}
@Override
@Bean
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}
}
这是我的 KeycloakConfig 如下所示。
@Configuration
public class KeycloakConfig {
@Bean
public KeycloakConfigResolver keycloakConfigResolver(){
return new KeycloakSpringBootConfigResolver();
}
}
这是我的 Controller class 如下所示。
@RestController
@RequestMapping("/api/v1/user_role")
@RequiredArgsConstructor
public class UserController {
private final Logger LOGGER = LoggerFactory.getLogger(getClass());
private final UserService userService;
@GetMapping("/alladvertisements")
public ResponseEntity<List<Advertisement>> getAllAdvertisements(){
LOGGER.info("UserController | getAllAdvertisements is started");
return ResponseEntity.ok(userService.getAllAdvertisements());
}
}
这是我的项目链接:链接
您正在使用的 Keycloak 适配器已弃用: https://github.com/keycloak/keycloak/discussions/10187 。
对于 spring-boot OpenID 资源服务器(REST API),您应该查看http://github.com/ch4mpy/spring-addons
我没有看到您的 CORS 配置
这意味着:
您之间可能存在不匹配(值必须完全相同)
iss
声明(使用https://jwt.io或类似方法打开它以获取值)运行 Docker 容器时的另一个可能问题是您的授权服务器(Keycloak)无法从资源服务器(Spring 微服务)与您配置的 URL 访问:从“外部”访问 Keycloak 容器以发出令牌,但也从“内部”由资源服务器验证这些令牌。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.