当前位置: 首页 > 知识库问答 >
问题:

如何在没有登录页面的情况下将Keycloak与Spring boot(API)集成

长孙弘壮
2023-03-14
@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) {
        KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
        keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
        auth.authenticationProvider(keycloakAuthenticationProvider);
    }

    @Bean
    public KeycloakSpringBootConfigResolver keycloakConfigResolver() {
        return new KeycloakSpringBootConfigResolver();
    }

    @Bean
    @Override
    protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
        return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http.authorizeRequests().antMatchers("/myapi*").hasRole("user").anyRequest().permitAll();
    }
}
server.port=8081
keycloak.auth-server-url=http://localhost:9080/auth
keycloak.realm=myrealm
keycloak.resource=myclient
keycloak.public-client=false
keycloak.credentials.secret=mysecret
keycloak.use-resource-role-mappings=true
keycloak.enabled=true
keycloak.ssl-required=external
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.api</groupId>
<artifactId>springboot-kc-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-kc-api</name>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.keycloak.bom</groupId>
            <artifactId>keycloak-adapter-bom</artifactId>
            <version>6.0.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <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.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
    </dependency>

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

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

每当发出GET请求时,keycloak调试将显示以下日志:

O.K.Adapters.oAuthRequestAuthenticator:发送重定向到登录页面:http://localhost:9080/auth/realms/myrealm/protocol/openid-connect/auth?response_type=code&client_id=myclient&redirect_uri=http%3a%2f%2flocalhost%3a8081%2fmyapi%2fsampleget?param1=val1&state=a2b5072a-acb8-4bf6-8f33-b3f25deab492&login=true&scope=openid

Keycloak配置:

Client Protocol : openid-connect
Access Type : confidential
Valid Redirect URIs: http://localhost:8081/myapi/*

需要帮助来理解如何配置spring boot API以使用请求中的auth头来验证和授权请求。

共有1个答案

谢宸
2023-03-14

这可以通过启用bearer only模式来实现。首先通过application.properties在Spring Boot服务中启用此功能:

keycloak.bearer-only=true

有关此方面的更多细节,请参见[1]。

您还可以在客户端的管理控制台中强制执行此操作。

    null
 类似资料:
  • 我正在使用分子和流浪汉来部署centos7实例。出于某些原因,我需要使用ssh命令访问分子实例,。然后ssh信息将粘贴到我的VS代码扩充中。 Molecule.yml 上面的IP允许我访问vagrant外的80端口。 但是分子实例IP的ssh命令不起作用。 错误 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 警告:远程主机

  • 我正在尝试使用Cognito用户池、Facebook和Google设置用户注册和登录。我已经能够使用Cognito中的应用程序集成创建一个注册页面(通过遵循本指南),但我希望将注册页面与我的应用程序的其余部分一起托管。这个答案有些帮助,但缺乏细节。我真的找不到任何好的例子或文档。

  • 我有一个Spring boot项目,它有使用kafka进行日志记录的依赖项。我无法摆脱所述依赖项,因为我的逻辑需要来自该依赖项的代码 我已尝试禁用自动配置 我尝试通过bean配置将missingTopicsFatal设置为false 但很明显,设置已经是假的,因为我的应用程序运行时没有失败,只是tomcat不想打开侦听端口 应用程序启动,但spring拒绝打开侦听端口,并继续使用错误进行循环 在本

  • 我正在尝试将JSF Web应用程序与Spring Security集成。 目前我正在通过一种方法登录:在此方法中进行身份验证并根据用户重定向到目标页面。 登录页面(Login.xhtml): loginManagedBean。autenticar()(身份验证和重定向的方法): 如何替换此页面和方法以使用SpringSecurity? SpringSecurityConfig:

  • 我在文本7中使用pdfHTMl创建了文档。我想为没有第一页的每一页添加页眉。我使用了PdfDocument事件。END_PAGE事件。但它适用于有第一页的每一页。如何从第一页中删除页眉?谢谢。

  • 问题内容: 我正试图从这样打电话: 该代码可以正常编译,但是会失败。我不知道为什么我无法得到。有人可以帮我吗? 问题答案: 是输入输出参数。进行呼叫时,必须将其设置为缓冲区()的大小。它的类型也是,所以在Go中使用。Windows 类型是指向的指针(具有)。参见来源。 还返回3个值: 存储返回并打印。完成此操作后,您会更早发现错误: 印刷品: 这意味着更多的数据比什么适合入缓冲区您传递可用- 或者