Spring Security是Spring Framework的应用程序安全解决方案。 Spring安全性可用于保护URL和方法调用。它广泛用于保护独立的Web应用程序,portlet和越来越多的REST应用程序。
3个微服务需要借助RestTemplate进行通信(交换数据)。 为此我分别开发了a、b、c微服务。他们注册到尤里卡服务器 和路由请求
如何使用Spring Security Java配置将HTTP请求重定向到HTTPS?
我有一个 Spring Security 版本 3.2.3 应用程序,它监听 HTTP 和 HTTPS。我希望对 HTTP 端口的任何请求都重定向到 HTTPS。如何仅使用 Java 进行配置? 春天
在我们的一项服务中,可以从两个来源获取请求:内部(在我们的集群内)和外部。对于内部请求,需要使用 JWT 令牌验证请求。对于前...
Spring Security oauth2Login 与 oauth2Client
我希望我的应用程序能够代表某个平台的用户发出 REST API 请求。我已经在平台上注册了我的应用程序,并且他们具有 OAuth2 支持,并具有以下 endp...
我对 Spring Security 中的“AuthenticationFilter.class”有疑问
http .csrf(AbstractHttpConfigurer::禁用) .cors(AbstractHttpConfigurer::禁用) .addFilterAfter(CoustomAuthCheckFilter, UsernamePasswordAuthenticationFilter.class) ... 我...
如何在 Spring Boot 中验证请求是否是从特定 Android 应用调用的
我使用 Spring Boot 和 Android 客户端应用程序开发了一个服务器应用程序。服务器为 Android 应用程序提供多个 API。 我需要在服务器端验证请求是否可以...
如何在 Spring Boot 3 中分别处理身份验证异常和类似 HTTP500 的基本异常
我正在使用 Spring Boot 3.0.1 和 Spring Security 构建一个 API,我已经构建了安全过滤器链并在其中使用自定义异常处理程序来处理身份验证中的异常(例如...
Spring 6 升级@PreAuthorize 无法与 ArguementResolver 一起使用
我正在对 Spring 6 升级进行一些测试,当我们升级到具有公共访问模式的环境时,PreAuthorize 装饰器失败。 用户提交带有保存请求的表单。 /
公共声明 extractClaims(字符串令牌){ SecretKey key = Keys.hmacShaKeyFor(secretKey.getBytes(StandardCharsets.UTF_8)); 返回 Jwts.parser() .setSigningKey(key)...
验证 JWT - Java17 Spring Security JWT:0.12.6
我正在做一个个人项目,我正在尝试设置一个具有多个 JWT 保护端点的 API。 我不知道 Spring Security 是如何工作的,所以我决定观看一个教程。 我已经结束了
我有一个非常标准的 Spring Boot 应用程序,我刚开始使用 JWT 使用 Spring Security。我已经在 Spring Boot 启动中添加了 GraphQL 支持(我已经在...
我想为我的弹簧控制器编写单元测试。我正在使用 keycloak 的 openid 流程来保护我的端点。 在我的测试中,我使用 @WithMockUser 注释来模拟经过身份验证的用户。我的公关...
为什么 spring-security.xml 中的 REST 配置不拦截 /rest 端点?
我正在使用 Spring 6.0 和 spring-security 6.1 这是我的 REST 配置 我正在使用 Spring 6.0 和 spring-security 6.1 这是我的 REST 配置 <security:http security-context-explicit-save="true" use-expressions="true" use-authorization-manager="false" pattern="/rest/**" create-session="stateless" entry-point-ref="restServicesEntryPoint"> <security:custom-filter ref="restServicesFilter" position="BASIC_AUTH_FILTER" /> </security:http> <bean id="restServicesEntryPoint" class="com.example.webservices.auth.RestAuthenticationEntryPoint"> <property name="realmName" value="Square" /> </bean> <bean id="restServicesFilter" class="com.example.webservices.auth.CustomRestSecurityFilter"/> 这是非休息配置 <security:http create-session="stateless" security-context-explicit-save="true" use-authorization-manager="false" authentication-manager-ref="authenticationManagerWithMultipleProviders" security-context-repository-ref="nullSecurityContextRepository"> <!-- Login config --> <security:access-denied-handler error-page="/bs/denied"/> <!-- Login config --> <security:form-login login-page="/bs/login" authentication-success-handler-ref="customAuthenticationSuccessHandler" authentication-failure-handler-ref="customAuthenticationFailureHandler" /> //other codes 当到达 /rest/** 端点时。它不会被 REST 安全配置拦截。它不是通过休息配置,而是通过非休息配置。 如果删除属性use-authorization-manager=false,则会出现循环依赖错误。 在 Spring 6.x 版本中,配置已将定义 bean 从基于 XML 的配置更改为使用 @Configuration 类和 SecurityFilterChain 的基于 Java 的配置。这种方法更加灵活,并且更符合现代 Spring 实践。 REST 安全配置 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.web.SecurityFilterChain; @Configuration @EnableWebSecurity public class RestSecurityConfig { @Bean public SecurityFilterChain restSecurityFilterChain(HttpSecurity http) throws Exception { http .securityContext().securityContextRepository(new NullSecurityContextRepository()).and() .securityContext(context -> context .requireExplicitSave(true)) .authorizeHttpRequests(authorize -> authorize .requestMatchers("/rest/**").authenticated()) .sessionManagement(session -> session .sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .exceptionHandling(exception -> exception .authenticationEntryPoint(restServicesEntryPoint())) .addFilterBefore(restServicesFilter(), BasicAuthenticationFilter.class); return http.build(); } @Bean public RestAuthenticationEntryPoint restServicesEntryPoint() { RestAuthenticationEntryPoint entryPoint = new RestAuthenticationEntryPoint(); entryPoint.setRealmName("Square"); return entryPoint; } @Bean public CustomRestSecurityFilter restServicesFilter() { return new CustomRestSecurityFilter(); } } 非 REST 安全配置 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.web.SecurityFilterChain; @Configuration @EnableWebSecurity public class NonRestSecurityConfig { @Bean public SecurityFilterChain nonRestSecurityFilterChain(HttpSecurity http) throws Exception { http .securityContext(context -> context .requireExplicitSave(true)) .sessionManagement(session -> session .sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .formLogin(form -> form .loginPage("/bs/login") .successHandler(customAuthenticationSuccessHandler()) .failureHandler(customAuthenticationFailureHandler())) .exceptionHandling(exception -> exception .accessDeniedHandler(accessDeniedHandler())); return http.build(); } @Bean public CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler() { return new CustomAuthenticationSuccessHandler(); } @Bean public CustomAuthenticationFailureHandler customAuthenticationFailureHandler() { return new CustomAuthenticationFailureHandler(); } @Bean public AccessDeniedHandler accessDeniedHandler() { AccessDeniedHandlerImpl handler = new AccessDeniedHandlerImpl(); handler.setErrorPage("/bs/denied"); return handler; } } 主要配置 @Configuration public class MainSecurityConfig { @Order(1) @Bean public SecurityFilterChain restSecurityFilterChain(HttpSecurity http) throws Exception { return new RestSecurityConfig().restSecurityFilterChain(http); } @Order(2) @Bean public SecurityFilterChain nonRestSecurityFilterChain(HttpSecurity http) throws Exception { return new NonRestSecurityConfig().nonRestSecurityFilterChain(http); } }
如何使用 Spring Boot 保存与数据库中已存在的特定作者对象相对应的书籍对象
我在 Spring Boot 中创建了一个项目,即 book-store 。我有一项后期服务,用于获取书籍和作者的详细信息并将其保存到数据库中[书籍和作者同时...
spring-boot 3.3.2 中的自定义身份验证过滤器在身份验证后始终重定向到“/”
我正在尝试创建一个自定义身份验证过程,该过程将根据请求中找到的信息对用户进行身份验证,并为他们创建一个会话,以便后续请求不需要...
带有 DefaultOAuth2AuthorizedClientManager 的 WebClient 适用于集成测试,但不适用于主运行时
我的 WebClient 配置如下。在主运行时,webClient 在 kafka 侦听器启动的线程中使用。所以脱离了 http servlet 上下文。它失败了,因为我注意到我们应该使用
身份验证期间哪些因素驱动 okta-spring-boot 中的 redirect_uri 设置?
我正在与可怕的人战斗 400 错误请求错误:“redirect_uri”参数必须是客户端应用程序设置中的登录重定向 URI 我的 Maven 父级是 spring-boot-starter-parent 3.3.2 并且我已经...
Spring Boot / Spring Security Cookie - 重命名时出现问题 - 而且值与会话 ID 不同
我有一个 Spring Auth 服务器(servlet),并且想要重命名与会话 id 关联的 cookie 的名称。 如果我这样做,则没有任何效果: @配置 内部类 CookiesConfig { @B...
我使用 spring-security 进行 OAuth2 身份验证和授权,这是一个类似的过滤器链示例 公共SecurityFilterChain securityFilterChain(HttpSecurity http)抛出异常{
我需要添加一些自定义逻辑,例如使用服务来记录我的失败尝试或在成功登录时重置我的失败尝试。如何使用 httpBasic 执行此操作?所以我需要一个失败处理程序和su...