【问题标题】:migrate to keycloak from spring boot security从 Spring Boot 安全迁移到 keycloak
【发布时间】:2017-08-07 07:20:06
【问题描述】:

我想从旧的 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());
        }
}

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

【问题讨论】:

    标签: spring-boot keycloak


    【解决方案1】:

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

    <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>
    

    其余的可以在您的 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/*
    

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

    【讨论】:

    • 是的,我已经启动了 keycloak。但问题是所有用户的凭据都在我的数据库上,而不是在 keycloak 上。那么如何使用我的数据库进行 keycloak 检查以进行身份​​验证和授权?
    • 您可以编写自定义提供程序,我以前没有自己做过,但应该可以。有关一些信息,请参阅keycloak.gitbooks.io/server-adminstration-guide/content/topics/…,也许谷歌一点。一般来说,应该有一个你需要实现的接口,这个实现将进入你的数据库并查询那里给出的用户数据。唯一的问题是您是否可以提供界面所需的所有数据,但您需要检查一下。
    • 我刚刚找到了一个更好的例子,使用一些与 JPA 连接的 DB,最后参见 github.com/keycloak/keycloak/tree/master/examples/providers/… 这也是在 keycloak 中具有这种 FDI 功能的一大优势,因为您可以插入多个提供程序和一起使用。
    • 我会检查一下,你有没有找到任何 spring boot 的例子??
    猜你喜欢
    • 1970-01-01
    • 2019-10-09
    • 2019-01-07
    • 2020-11-18
    • 2022-01-25
    • 1970-01-01
    • 2022-10-18
    • 2022-09-28
    • 2020-07-07
    相关资源
    最近更新 更多