Keycloak 将我重定向到我的索引 url 而不是请求的
Posted
技术标签:
【中文标题】Keycloak 将我重定向到我的索引 url 而不是请求的【英文标题】:Keycloak redirects me to my index url instead of to the requested one 【发布时间】:2016-02-06 05:38:55 【问题描述】:我正在使用 Keycloak 服务器 (v 1.5.1) 对我的服务执行类似 open-id-connect 的身份验证。我已经建立了一个基本的 Web 应用程序,它有两个 url,一个是 /index.html,另一个是 /hello。我使用 Spring security、Spring boot 和 Spring MVC 来解决所有这些问题。这是我的 pom.xml 配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.keycloaktes</groupId>
<artifactId>keycloaktes</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-security-adapter</artifactId>
<version>1.5.1.Final</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-tomcat8-adapter</artifactId>
<version>1.5.1.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
当我在未登录时访问 /hello url 时出现问题,keycloak 登录屏幕正确显示,但成功后没有执行重定向到 /hello登录,它会在我的 /index.html 页面上执行此操作。这就是我配置安全适配器的方式:
@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
throws Exception
auth.authenticationProvider(keycloakAuthenticationProvider());
/**
* Defines the session authentication strategy.
*/
@Bean
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy()
return new RegisterSessionAuthenticationStrategy(
new SessionRegistryImpl());
@Bean
public FilterRegistrationBean keycloakAuthenticationProcessingFilterRegistrationBean(
KeycloakAuthenticationProcessingFilter filter)
FilterRegistrationBean registrationBean = new FilterRegistrationBean(
filter);
registrationBean.setEnabled(false);
return registrationBean;
@Bean
public FilterRegistrationBean keycloakPreAuthActionsFilterRegistrationBean(
KeycloakPreAuthActionsFilter filter)
FilterRegistrationBean registrationBean = new FilterRegistrationBean(
filter);
registrationBean.setEnabled(false);
return registrationBean;
@Override
protected void configure(HttpSecurity http) throws Exception
super.configure(http);
http.authorizeRequests().antMatchers("/*").hasRole("ADMIN")
.anyRequest().permitAll();
http.csrf().disable();
我一直在尝试启用KeycloakAuthenticationProcessingFilter
和KeycloakPreAuthActionsFilter
,但结果保持不变。有人知道如何解决这个问题吗?
【问题讨论】:
【参考方案1】:您必须允许访问 SSO 登录入口点。
@Override
protected void configure(HttpSecurity http) throws Exception
super.configure(http);
http.authorizeRequests()
.antMatchers("/sso/login*").permitAll()
.antMatchers("/*").hasRole("ADMIN")
.anyRequest().permitAll();
http.csrf().disable();
Spring Security 角色按照声明它们的顺序进行评估,因此允许访问 /sso/login
必须在更严格的规则之前。
【讨论】:
以上是关于Keycloak 将我重定向到我的索引 url 而不是请求的的主要内容,如果未能解决你的问题,请参考以下文章