SpringBoot项⽬启动成功访问任意接⼝都跳转到login登录页⾯1.1 Bug场景:
1.1 SpringBoot项⽬启动成功访问任意接⼝都跳转到login登录页⾯
2.1Bug原因
在 SpringBoot 项⽬中使⽤了 SpringSecurity ,这是因为在SpringBoot中,默认的Spring Security就是⽣效了的,此时的接⼝都是被保护的,我们需要通过验证才能正常的访问。Spring Security提供了⼀个默认的⽤户,⽤户名是user,⽽密码则是启动项⽬的时候⾃动⽣成的。
我们查看项⽬启动的⽇志,会发现控制台有如下的⼀段Log
3.1 解决⽅法:
1. 如果不需要使⽤ SpringSecurity 去掉以依赖从新启动项⽬就可以
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2. 如果需要使⽤ SpringSecurity ,就是⽤ user + ⽣成的密码进⾏登陆,登陆成功后就跳转正常的调⽤页⾯。
或者对spring boot1.5配置security关闭http基本验证,只需要在application.properites中配置即可,但是spring boot 2.0+之后这样配置就不能⽣效了。
abled=false
3. 在项⽬中添加⼀个配置类(推荐使⽤第三种)
package com.fig.security;
import t.annotation.Configuration;
import org.fig.annotation.web.builders.HttpSecurity;
import org.fig.figuration.EnableWebSecurity;
import org.fig.figuration.WebSecurityConfigurerAdapter;
/**
* 访问接⼝不在调⽤security
* @author Liyh
* @date 2020/12/22
*/
springframework依赖
@Configuration
@EnableWebSecurity
public class CloseSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//figure(http);
http.csrf().disable();
//配置不需要登陆验证
http.authorizeRequests().anyRequest().permitAll().and().logout().permitAll();
} }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。