【发布时间】:2018-09-20 21:46:05
【问题描述】:
我已通过基本身份验证保护了我的 Spring Boot 应用程序。下面是我的 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);
}
}
当我尝试通过邮递员访问应用程序休息 URL 时遇到问题。当我没有在邮递员的授权中提供任何授权/错误凭据时,我无法访问资源并获得无效凭据错误消息。但是,问题是当我第一次能够使用正确的凭据成功访问资源时,现在我将凭据更改为无效的凭据并尝试访问该资源,我仍然能够访问它。
我可以通过任何方式防止这种情况发生。
提前致谢。
问候, 法尔汉
【问题讨论】:
标签: spring-boot spring-security