项目场景:
笔者最近有一个项目,依赖关系如下图:
- main-project 是主项目
- auth-project 是公司封装好的认证中心项目,打包成了jar包供引用,基于spring security开发
需求描述
现在笔者需要在main-project上做开发,即在Controller中添加一个新接口,但是由于引用了auth-project,所以所有接口必须要先登录获取token之后才能访问。
为了便于开发,笔者想暂时跳过spring security的认证过程。
解决方案:
方法一:
在main-project的config包下编写一个类,继承WebSecurityConfigurerAdapter,并重写configure(HttpSecurity http)方法,放行所有请求,代码如下:
@EnableWebSecurity
@Order(1)
public class MyWebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeHttpRequests()
.anyRequest().permitAll()
.and()
.csrf().disable();
}
这里有2点需要注意:
- @Order(1)是为了配置执行顺序的优先级,因为auth-project里已经有了一个WebSecurity的配置类,所以这里配置@Order(1)是为了让我们写的配置类先加载,数字越小优先级越高;
- 需要禁用csrf防御,否则除get请求方法以外,其它类型的请求会返回403响应。
方法二:
在main-project的pom.xml文件中,注释掉auth-project的dependency。
参考文章:
浅谈Spring @Order注解的使用
Spring Boot请求403 Forbidden错误
本文如有侵权,请联系我删除,谢谢!