gpt4 book ai didi

spring-boot - Spring boot + keycloak 应用程序在部署到 tomcat 时不起作用

转载 作者:行者123 更新时间:2023-12-04 10:13:20 26 4
gpt4 key购买 nike

我已经将 keycloak 与我的 Spring Boot 应用程序集成在一起。当我使用 spring boot run 运行它时,它正在工作。但是当我创建一个 war 并部署到 tomcat 时,我得到一个异常:

java.lang.NullPointerException: null
at org.keycloak.adapters.KeycloakDeploymentBuilder.internalBuild(KeycloakDeploymentBuilder.java:57) ~[keycloak-adapter-core-9.0.2.jar:9.0.2]

我需要做些什么才能将它部署到 tomcat?

这是我的 application.properties文件:
# keycloak configuration
keycloak.realm=salary-plus
keycloak.auth-server-url=http://localhost:8080/auth
keycloak.ssl-required=external
keycloak.resource=salary-plus-api
keycloak.credentials.secret=8dd03031-60ac-4ef5-9ae5-19a2e3460da2
keycloak.use-resource-role-mappings = true

这是我的 spring 安全配置类:
@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {

@Autowired
public void configureGlobal(
AuthenticationManagerBuilder auth) throws Exception {

KeycloakAuthenticationProvider keycloakAuthenticationProvider
= keycloakAuthenticationProvider();
keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(
new SimpleAuthorityMapper());
auth.authenticationProvider(keycloakAuthenticationProvider);
}

@Bean
public KeycloakSpringBootConfigResolver KeycloakConfigResolver() {
return new KeycloakSpringBootConfigResolver();
}

@Bean
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new NullAuthenticatedSessionStrategy();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http
.csrf()
.disable();
http.authorizeRequests()
.antMatchers(HttpMethod.GET, "/api/disbursement/**").hasAnyRole(
"disbursement_maker", "disbursement_checker")
.antMatchers(HttpMethod.POST, "/api/disbursement/request").hasRole("disbursement_maker")
.antMatchers(HttpMethod.PUT, "/api/disbursement/item/*").hasRole("disbursement_maker")
.antMatchers(HttpMethod.PATCH, "/api/disbursement/request/update/*").hasRole("disbursement_maker")
.antMatchers("/api/auth/**").permitAll()
.anyRequest()
.denyAll();
}
}

最佳答案

要部署为 WAR 文件,您需要使用 Keycloak Spring Security 适配器或 Tomcat 适配器,而不是 Spring Boot 适配器。来自 docs :

If you plan to deploy your Spring Application as a WAR then you should not use the Spring Boot Adapter and use the dedicated adapter for the application server or servlet container you are using. Your Spring Boot should also contain a web.xml file.

关于spring-boot - Spring boot + keycloak 应用程序在部署到 tomcat 时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61209657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com