我已经用基本身份验证来保护我的spring引导应用程序。下面是我的春季安全配置。
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);
}
}当我试图通过postman访问应用程序rest URL时,我会遇到一些问题。当我在“邮递员”中没有授予任何授权/错误的授权凭证时,我无法访问该资源并获得无效的凭据错误消息。但是,问题是,当我第一次能够使用正确的凭据成功地访问资源时,现在我将凭据更改为无效的凭据并尝试访问资源时,我仍然能够访问它。
任何我都能阻止的方法。
提前谢谢。
你好,法尔汉
发布于 2018-09-20 13:46:05
Spring Security创建一个会话,并将Postman保存的cookie发回,然后登录,直到此会话无效。
您可以使用以下命令禁用会话:
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);在SpringSecurityConfiguration的配置方法中。
但是请记住,您禁用了整个会话管理。如果您只有一个REST,可能会很好,但是如果您还有其他web组件,您可能需要HTTP会话。
https://stackoverflow.com/questions/52426341
复制相似问题