Java 在Spring引导中保护根URL
我正在尝试使用keydove保护Spring引导应用程序,并通过Java而不是XML对其进行配置。我的SecurityConfig类如下所示: @配置 @启用Web安全性 @ComponentScanbasePackageClasses=keydeposecurityComponents.class, excludeFilters=@ComponentScan.Filtertype=Filtertype.REGEX,pattern=org.keydape.adapters.springsecurity.management.HttpSessionManager 公共类SecurityConfig扩展了KeyDopperWebSecurity配置适配器{ @自动连线 公共无效配置GlobalAuthenticationManagerBuilder身份验证{ keydapertificationprovider keydapertificationprovider=keydapertificationprovider; KeyClope AuthenticationProvider.setGrantedAuthoritiesMappernew SimpleAuthorityMapper; auth.authenticationProviderKeyMaveAuthenticationProvider; } @豆子 公钥斗篷SpringBootConfigResolver密钥斗篷ConfigResolver{ 返回新的keydepospringbootconfigResolver; } @豆子 @凌驾 受保护的SessionAuthenticationStrategy SessionAuthenticationStrategy{ 返回新的RegisterSessionAuthenticationStrategynew SessionRegistryImpl; } @凌驾 受保护的void configureHttpSecurity http引发异常{ super.configurehttp; http.authorized请求 .anyRequest.hasRoleadmin; } } 根据我的理解,如果用户当前未登录,那么对anyRequest的调用应该确保所有请求都被重定向到keydape登录页面。但是,这似乎只适用于不是根URL的URL。项目当前有两个端点://同步。尝试正确访问同步端点会重定向到登录页面。尝试访问根页面不起作用,因此它似乎被忽略。我也尝试过使用antMatchers*、**、/、/*、/**.hasRoleadmin,但这些模式似乎都不会导致根URL受到保护 罪魁祸首似乎是对super.configurehttp的调用。但是,这会执行一些相对重要的操作,如CSRF保护、登录/退出端点等。以下是提取到方法中的等效代码,而不是超级调用: @凌驾 受保护的void configureHttpSecurity http引发异常{ http.csrf.requireCsrfProtectionMatcherthis.keydeposecsrrfrequestmatcher .and.sessionManagement.sessionAuthenticationStrategythis.sessionAuthenticationStrategy .and.ADDFILTERBEFORTHS.KEYOVERPREAUTHACTIONSFILTER,LogoutFilter.class .addFilterBeforthes.keydepeauthenticationProcessingFilter,BasicAuthenticationFilter.class .addFilterBeforthes.keydapertificatedActionsFilter,BasicAuthenticationFilter.class .addFilterAfterthis.Key隐形SecurityContextRequestFilter,SecurityContextHolderAwareRequestFilter.class .exceptionHandling.authenticationEntryPointthis.authenticationEntryPoint异常处理 .and.logout.addlogouthandler this.keydeposetlogouthandler.logoutUrl/sso/logout.permitAll.logoutSuccessUrl/; http.authorized请求 .anyRequest.hasRoleadmin; }Java 在Spring引导中保护根URL,java,spring,spring-boot,keycloak,Java,Spring,Spring Boot,Keycloak,我正在尝试使用keydove保护Spring引导应用程序,并通过Java而不是XML对其进行配置。我的SecurityConfig类如下所示: @配置 @启用Web安全性 @ComponentScanbasePackageClasses=keydeposecurityComponents.class, excludeFilters=@ComponentScan.Filtertype=Filtertype.REGEX,pattern=org.keydape.adapters.springsecur
颠倒超级调用和我自己的授权代码之间的顺序也不会导致根URL受到保护。我刚刚遇到同样的问题,您需要将logoutSuccessUrl更改为您想要保护的/以外的其他内容
.and().logout().addLogoutHandler(this.keycloakLogoutHandler()).logoutUrl("/sso/logout").permitAll().logoutSuccessUrl("/");
到
默认情况下,logoutSuccessUrl为/,因此是不安全的,为了保护/您需要更改它
.and().logout().logoutSuccessUrl("/logout-success")
我还建议您使用KeyClope spring启动程序如果删除super.configurehttp;?这解决了问题,但增加了不少新问题;超类配置重要的东西。super.configurehttp执行的代码太长,因此我将稍微编辑一下这个问题。以相反的顺序调用它们如何?超级调用似乎是正确的方向,但无论是提取它并将其部分移动到代码之外,还是将整个超级调用移到下面,都不会产生预期的效果。唯一的工作是手动转换为XML配置,但我不认为这是一个解决方案,因为它在Spring引导上下文中被劝阻。
.and().logout().logoutSuccessUrl("/logout-success")