【解决】SpringBoot引入Spring security后发布项目弹出登录框

问题描述

最近在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);
    }
}

如果说你已经添加配置类,这个页面恰恰说明你的配置类没有起作用,可能是您的工程没有重新编译,需要重新编译项目,然后再发布。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值