当前位置:   article > 正文

【解决】SpringBoot引入Spring security后发布项目弹出登录框_spring-boot-starter-security有登录对话框

spring-boot-starter-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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

添加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);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/316566?site
推荐阅读
相关标签
  

闽ICP备14008679号