spring-boot - Spring Boot - Spring 安全授权问题
问题描述
我已经通过基本身份验证保护了我的 Spring Boot 应用程序。下面是我的弹簧安全配置。
package com.exxonmobil.asr.backoffice.configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
@Configuration
@EnableWebSecurity
public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter {
private static final String SPRING_SECURITY_PASSWORD = "spring.security.password";
private static final String SPRING_SECURITY_USERNAME = "spring.security.username";
@Autowired
private AuthenticationEntryPoint authEntryPoint;
@Autowired
private Environment env;
@Bean
public PasswordEncoder bcryptPasswordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser(env.getProperty(SPRING_SECURITY_USERNAME))
.password(bcryptPasswordEncoder().encode(env.getProperty(SPRING_SECURITY_PASSWORD))).roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests().antMatchers("/**").hasAnyRole("USER").anyRequest().authenticated().and().httpBasic()
.authenticationEntryPoint(authEntryPoint);
}
}
当我尝试通过邮递员访问应用程序休息 URL 时遇到问题。当我没有在邮递员的授权中提供任何授权/错误凭据时,我无法访问资源并获得无效凭据错误消息。但是,问题是当我第一次能够使用正确的凭据成功访问资源时,现在我将凭据更改为无效的凭据并尝试访问该资源,我仍然能够访问它。
任何方式我都可以防止这种情况。
提前致谢。
问候,法尔汉
解决方案
Spring Security 创建一个会话并发送回 Postman 保留的 cookie,然后您登录,直到该会话无效。
您可以使用以下方法禁用会话:
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
在 SpringSecurityConfiguration 的 configure 方法中。
但请记住,您禁用了整个会话管理。如果您只有一个 REST API 可能没问题,但如果您还有其他 Web 组件,则可能需要 HTTP 会话。
推荐阅读
- c# - 使用 Kerberos Change-Password 协议和 .NET Core 3 更改 Active Directory 密码
- c++ - 如何检查二进制文件中是否存在字符串?
- loops - 在 for 循环期间密钥未存储在 lua 表中
- java - 多次更改JPanel的背景颜色
- python - seaborn 中不同类型的猫图
- c++ - aes cbc 反向加密
- java - C 和 Java 中数据类型的大小?
- node.js - 如何删除 DynamoDB 中的多行?
- flutter - Flutter 与 Car Play 和 Android Auto 的兼容性
- javascript - 我们如何在 html 中为网页添加循环音乐?