spring|springboot使springsecurity不用登录

在springboot中加入springsecurity的依赖

org.springframework.boot spring-boot-starter-security

当引入这个依赖后,启动项目,springSecurity也会跟着启动,默认使用HttpBasic的方式启动,用户名是user,密码在启动日志里面,是个随机字符串。
spring|springboot使springsecurity不用登录
文章图片

其中5ced945c-3f81-4e08-baab-0c02606971e1为登录密码。
有时候暂时不需要springSecurity服务时,例如直接访问一个接口,有登录拦截的话会访问不了,此时需要对springSecurty进行配置,放行所有的url。
配置如下:
package com.example.demo.config; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; /** * @author shanpeng */ @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { // 放行所有请求 @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().permitAll(); } }

【spring|springboot使springsecurity不用登录】完成配置后,实际效果上就是使springSecurity不生效。

    推荐阅读