首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从spring boot安全迁移到keycloak

从spring boot安全迁移到keycloak
EN

Stack Overflow用户
提问于 2017-03-16 16:19:13
回答 1查看 2.8K关注 0票数 6

我想从我的旧spring boot迁移到keycloak,安全配置是app.Below。

代码语言:javascript
复制
@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());
        }
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-16 23:48:26

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

代码语言:javascript
复制
<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以及应用程序的哪些部分应该受到保护。这可能看起来像

代码语言:javascript
复制
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上找到一个非常好的教程,它是我所描述的较长版本。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42828627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档