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

密钥斗篷登录页面不显示,我得到Spring安全登录

段弘和
2023-03-14

嗨,我正在尝试用我已经开始的Java15为静态编程语言Spring Boot项目添加安全性。我想使用KeyCloak作为验证服务器。

我还没有前端(REACT应用程序),我首先构建REST API。

我的问题是,当我尝试点击受保护的endpoint而不是使用Keycloak登录页面时,我想会弹出Spring Security登录页面,因为它显示无效凭据,并且外观是基本形式,而不是Keycloak用于登录的样式。我不知道我的配置中缺少什么。

这是SecurityConfig:

@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = [KeycloakSecurityComponents::class])
internal class SecurityConfig : KeycloakWebSecurityConfigurerAdapter() {
    @Autowired
    fun configureGlobal(auth: AuthenticationManagerBuilder) {
        val keycloakAuthenticationProvider: KeycloakAuthenticationProvider =
            keycloakAuthenticationProvider()
        keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(
            SimpleAuthorityMapper()
        )
        auth.authenticationProvider(keycloakAuthenticationProvider)
    }

    @Bean
    fun keycloakConfigResolver(): KeycloakSpringBootConfigResolver {
        return KeycloakSpringBootConfigResolver()
    }

    @Bean
    override fun sessionAuthenticationStrategy(): SessionAuthenticationStrategy {
        return RegisterSessionAuthenticationStrategy(
            SessionRegistryImpl()
        )
    }

    override fun configure(http: HttpSecurity) {
        super.configure(http)
        http.authorizeRequests()
            .antMatchers("/carts*")
            .hasRole("user")
            .anyRequest()
            .permitAll()
    }
}

控制器:

@RestController
@RequestMapping("/carts")
class CartController(private val cartService: CartService) {

    @GetMapping("/{id}")
    fun getCart(@PathVariable id: String): Cart? {
        return cartService.findById(id)
    }
}

应用程序yml:

keycloak:
  auth-server-url: http://localhost:8081/auth
  realm: TestRealm
  resource: login-app
  public-client: true
  principal-attribute: preferred_username

建筑渐变相关性:

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.12.+")
    implementation("org.keycloak:keycloak-spring-boot-starter")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.springframework.security:spring-security-test")
    testImplementation("io.mockk:mockk:1.10.6")
}

dependencyManagement {
    imports {
        mavenBom("org.keycloak.bom:keycloak-adapter-bom:12.0.4")
    }
}

当我直接请求时,我会得到一个有效的令牌,所以我假设KeyClope正在工作(我在8081端口的docker容器上运行它)。

curl --location --request POST 'http://localhost:8081/auth/realms/TestRealm/protocol/openid-connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=login-app' \
--data-urlencode 'username=user2' \
--data-urlencode 'password=user2' \
--data-urlencode 'grant_type=password'

对“localhost:8080/carts/605271fa9f2ad0418ca4858d”的请求重定向到http://localhost:8080/login“这表明:

我看到的每个指南都被重定向到开箱即用的密钥斗篷登录。我有点迷路了,有什么想法吗?

谢谢

共有2个答案

葛雪松
2023-03-14

因为没有将包添加到我的SecurityConfig(从其他地方复制了该示例,可能是重写了包定义)类中而失去了下午的时间,因此组件扫描完全跳过了该配置。

然而,我让它工作的方式是直接将令牌请求到keycloak url,并将其与头一起发送-头'授权:承载eyJhbGciOiJSUzI1NiIsInR5cCIgOiA...'

我猜KeyClope登录页面是我以后必须直接从前端配置的,因为我没有使用Spring MVC

越俊驰
2023-03-14

为了安全起见你必须禁用自动配置

在你的Main类中使用这个

@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class})
 类似资料:
  • 登录到Keycloak Jaeger(realm)客户端后,Keycloak服务器没有导航到Jaeger UI路径- 似乎钥匙斗篷验证了用户(请参阅下面的代码) proxy.json keycloak.json

  • 我正试图在我的spring boot应用程序中使用KeyClope。 我想根据REST方法和用户角色限制对特定URL的访问。 在下面的示例中,具有或角色的用户可以执行GET,而具有或角色的用户可以执行POST、PUT或DELETE。 不幸的是,此配置允许任何经过身份验证的用户访问/api/日历URL。我做错了什么?

  • 我有一个使用SpringMVC的Java网络应用程序。该 Web 应用程序在 jboss AS7.1 服务器上运行,该服务器使用带有表单认证的 JAAS 登录模块。当用户在表单上填写其用户名和密码时,登录工作顺利。 我现在想创建一个java控制器,它“将用户登录”,就好像用户填写了日志通知一样。 当上述方法完成时,应允许对任何安全页面的任何访问,因为用户被视为已登录。 可以通过访问Jboss的lo

  • 我已将Websphere Liberty服务器配置为支持IBM知识中心中所述的管理中心。 我在Eclipse Neon环境(即localhost)的Windows 10上运行WASLiberty版本17.0.0.1。我从Chrome(版本61)连接URL(http://localhost:9080/adminCenter)。 它会显示一个登录页面(外观不好看,但可用) 我输入我的凭据并单击“提交”

  • 通过设置安全码,从而达到隐藏登录页的效果,保证页面的私有性。 /app/Application/Admin/Conf/config.php return array( 'LOGIN_MAX_FAILD' => 5, 'BAN_LOGIN_TIME' => 30, //登录失败5次之后需等待30分钟才可再次登录 'ADMIN_PANEL_SECURITY_CODE' => '

  • 部署在AWS上,我不想隧道到盒子并打开浏览器禁用它。 似乎存在一个配置:,可以放置在文件,但我不确定在哪个对象下。我在“王国”下试过,但一点运气都没有。 我不想在适配器级别禁用它,它需要全局禁用,那么在哪里,或者如何全局禁用ssh/https? 另外,我知道这在生产中是不推荐的。)