Ske*_*eve 8 java spring-security spring-boot keycloak
我需要将身份验证添加到我的 Spring Boot (MVC) 应用程序中。身份验证提供者是通过 OpenID 的 keycloak。隐式授予和授权代码授予均被禁用,因此我只能使用资源所有者凭据授予。我想要实现的是针对未经授权的用户的基本身份验证提示。以这种方式检索到的凭证应用于从 keycloak 获取令牌和用户信息,以便 spring security 进一步使用。应在每个请求时检查令牌。
我发现的大多数示例都使用org.keycloak:keycloak-spring-boot-starter. enable-basic-auth 虽然我在这里找到了,但它对我不起作用。我知道我必须使用它keycloak.bearer-only=true来关闭重定向,但它可以正常工作,因此它不会为未经授权的用户返回 401,而是返回 401。
我的安全配置:
@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
@Bean
public KeycloakSpringBootConfigResolver KeycloakConfigResolver() {
return new KeycloakSpringBootConfigResolver();
}
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().permitAll();
}
}
Run Code Online (Sandbox Code Playgroud)
我的 keycloak 属性(我不使用占位符,这只是为了安全起见):
keycloak.auth-server-url=https://${keycloak.host}/auth/
keycloak.realm=master
keycloak.resource=${client.name}
keycloak.enable-basic-auth=true
keycloak.credentials.secret=${client.secret}
Run Code Online (Sandbox Code Playgroud)
很抱歉问这个一般性问题。我主要使用 jdbcAuthentication,这是我第一次使用身份管理软件。
这是通过 keycloak openid 使用资源所有者密码凭据流程向应用程序添加基本身份验证的一种方法。
首先,您需要将其添加到 pom.xml 中:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.keycloak.bom</groupId>
<artifactId>keycloak-adapter-bom</artifactId>
<version>4.2.0.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
...
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-boot-starter</artifactId>
</dependency>
...
</dependencies>
Run Code Online (Sandbox Code Playgroud)
添加keycloak服务器属性:
keycloak.auth-server-url=https://${keycloak.host}/auth/
keycloak.realm=master
keycloak.resource=${client.name}
keycloak.credentials.secret=${client.secret}
keycloak.enable-basic-auth=true
keycloak.bearer-only=true
Run Code Online (Sandbox Code Playgroud)
enable-basic-auth实际上负责启用资源所有者密码凭据流。因此,每次您使用基本授权发送请求时BasicAuthRequestAuthenticator,都会从 keycloak 请求令牌。bearer-only需要关闭访问代码流。每次您在没有基本授权(以及外部授权会话)的情况下向安全资源发出请求时,您都会被重定向到 keycloak 服务器 - 我们不希望这样。有了bearer-only你就会得到401。
最后一步是添加安全配置:
@Configuration
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
@EnableWebSecurity
public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
@Bean
public KeycloakAuthenticationProvider authenticationProvider() {
KeycloakAuthenticationProvider provider = new KeycloakAuthenticationProvider();
provider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
return provider;
}
@Bean
public KeycloakSpringBootConfigResolver KeycloakConfigResolver() {
return new KeycloakSpringBootConfigResolver();
}
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http
.csrf().disable()
.exceptionHandling().authenticationEntryPoint((request, response, authException) -> {
response.addHeader(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"Restricted Content\"");
response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
})
.and()
.authorizeRequests()
.antMatchers("/admin/**").hasRole("admin")
.anyRequest().permitAll();
}
}
Run Code Online (Sandbox Code Playgroud)
这里最有趣的部分是:
.exceptionHandling().authenticationEntryPoint((request, response, authException) -> {
response.addHeader(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"Restricted Content\"");
response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
})
Run Code Online (Sandbox Code Playgroud)
标准 keycloakAuthenticationEntryPoint实现将WWW-Authenticate标头设置String.format("Bearer realm=\"%s\"", realm)为以防授权失败。我需要将其设置Basic realm="Restricted Content"为弹出基本身份验证提示。如果您想避免使用此提示(您想添加自己的登录表单) - 删除此部分。
| 归档时间: |
|
| 查看次数: |
6454 次 |
| 最近记录: |