部署到tomcat的Spring Boot + Keycloak应用程序无法正常工作

时间:2020-04-14 14:06:33

标签: spring-boot keycloak

我已经在我的Spring Boot应用程序中集成了密钥斗篷。当我使用spring boot run运行它时,它正在工作。但是当我发动战争并部署到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();
    }
}

1 个答案:

答案 0 :(得分:1)

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

如果您打算将Spring Application部署为WAR,则应该 不要使用Spring Boot Adapter并使用专用适配器 您正在使用的应用程序服务器或servlet容器。你的春天 引导程序还应包含一个web.xml文件。