问题描述
最近在SpringBoot开发过程中,使用了Spring Security包,但是引用后,后台项目发布时,出现了下面的界面
出现原因
其实这个是security默认给我们整的一个用户认证的功能,用户名是:user 密码是在启动的控制台打印出来的:
但是当输入用户名和密码后, 登录进去为404 说明我们什么服务也没有配置,要想配置自己的认证 ,需要添加一个继承WebSecurityConfigurerAdapter这个适配器的一个配置类。
解决方式
先说下Spring Boot 引入 Spring Security
<!--Spring security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
添加WebSecurityConfigurerAdapter的继承类
创建权限包及WebSecurityConfig类
具体代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
super.configure(auth);
}
@Override
public void configure(WebSecurity web) throws Exception {
super.configure(web);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
//关闭csrf跨域
http.csrf().disable();
//super.configure(http);
}
}
如果说你已经添加配置类,这个页面恰恰说明你的配置类没有起作用,可能是您的工程没有重新编译,需要重新编译项目,然后再发布。