从 spring boot security 迁移到 keycloak

boy*_*od3 6 spring-boot keycloak

我想从我的旧 Spring Boot 安全应用程序迁移到 keycloak。下面是我的安全配置。

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
    @Autowired
    private CustomUserDetailsService customUserDetailsService;
     @Override
        protected void configure(HttpSecurity http) throws Exception {
         http.csrf().disable();

         http
         .authorizeRequests()
             .antMatchers("/*", "/static/**", "/css/**", "/js/**", "/images/**").permitAll()
             .antMatchers("/school-admin/*").hasAuthority("SCHOOL_ADMIN").anyRequest().fullyAuthenticated()
             .antMatchers("/teacher/*").hasAuthority("TEACHER").anyRequest().fullyAuthenticated()
             .anyRequest().authenticated().and()

         .formLogin()
             .loginPage("/login.html").defaultSuccessUrl("/loginSuccess.html")
            .failureUrl("/login.html?error").permitAll().and()

         .logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout.html")).logoutSuccessUrl("/login.html?logout");

     }

     @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
         auth.userDetailsService(customUserDetailsService).passwordEncoder(new BCryptPasswordEncoder());
        }
}
Run Code Online (Sandbox Code Playgroud)

我已经安装了 keycloak 并且它在端口 8080 上运行。我发现问题是,我们应该在 keycloak 管理页面上创建角色和用户,但是我当前的系统是,用户和角色都在我的 MySQL 数据库上。我不想在 keycloak 上插入用户和角色以进行身份​​验证和授权。

hec*_*o84 2

好吧,显然第一件事是运行 keycloak 实例,我认为这应该可以通过在线文档来实现。我们在 Wildfly 实例上使用 ie Keycloak。下一步是在 keycloak 中定义一个领域和至少一个客户端,您将使用它来连接 spring-boot 应用程序。在应用程序的 POM 中,您需要添加 keylcoak 适配器的依赖项,例如 ie

<dependency>
    <groupId>org.keycloak</groupId>
    <artifactId>keycloak-tomcat8-adapter</artifactId>
</dependency>
<dependency>
    <groupId>org.keycloak</groupId>
    <artifactId>keycloak-spring-boot-adapter</artifactId>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

其余的可以在 application.properties 中完成,您可以在其中配置适配器如何连接到 keycloak 以及应保护应用程序的哪些部分。这看起来像

keycloak.realm=myrealm #realm that you have created in keycloak, contains your client
keycloak.auth-server-url=KeycloakHOST:KeycloakPort/auth # Substitute with your settings
keycloak.ssl-required=none
keycloak.resource=myclient
#keycloak.use-resource-role-mappings=true
keycloak.enable-basic-auth=true # we use basic authentication in this example
keycloak.credentials.secret=2dcf74ca-4e4f-44bf-9774-6c32c12783d3 # Secret generated for you client in keycloak
keycloak.cors=true
keycloak.cors-allowed-headers=x-requested-with,origin,content-type,accept,authorization
keycloak.cors-allowed-methods=GET,POST,DELETE,PUT,OPTIONS
keycloak.cors-max-age=3600
keycloak.expose-token=true
keycloak.bearer-only=true
keycloak.securityConstraints[0].securityCollections[0].name=adminRule
keycloak.securityConstraints[0].securityCollections[0].authRoles[0]=SCHOOL_ADMIN
keycloak.securityConstraints[0].securityCollections[0].patterns[0]=/school-admin/*
keycloak.securityConstraints[1].securityCollections[0].name=teacherRule
keycloak.securityConstraints[1].securityCollections[0].authRoles[0]=TEACHER
keycloak.securityConstraints[1].securityCollections[0].patterns[0]=/teacher/*
Run Code Online (Sandbox Code Playgroud)

这基本上就是您在 spring-boot 应用程序中需要做的全部事情。上述规则未涵盖的所有其他端点仍然可供所有人使用。您可以在这里找到一个非常好的教程,这是我所描述的较长版本。