SpringBoot访问接口出现401

SpringBoot访问接口出现401并且没有错误日志出现。发现以下两个错误

第一个问题:

网上查阅项目的pom.xml中多了spring-boot-starter-security的jar包,去掉这个jar包就好了

 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
  </dependency>

详情查看https://blog.csdn.net/hz_940611/article/details/80771832详情查看https://blog.csdn.net/hz_940611/article/details/80771832

第二个问题:

拦截器将访问接口拦截,所以出现401

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/logOut","/login","/getOutreachNumAndReceptionNum","/get/*","/set/*");
    }

在.excludePathPatterns()中配置不需要拦截器的地址即可。

修改完以上两个问题,便可以访问接口成功。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想要实现在访问接口时返回登录页,可以使用 Spring Security 来实现。 首先,你需要在 pom.xml 文件中引入 Spring Security 的依赖,如下所示: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 接着,在你的 SpringBoot 应用程序的配置类中添加 @EnableWebSecurity 注解,开启 Spring Security。 然后,你需要配置一个 WebSecurityConfigurerAdapter 类,这个类可以用来配置 Spring Security 的行为。在这个类中,你需要配置一个登录页面,以及哪些请求需要进行身份验证。 下面是一个示例: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/login").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .defaultSuccessUrl("/home") .permitAll() .and() .logout() .permitAll(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER"); } } ``` 在这个示例中,我们通过 configure 方法配置了登录页面和需要进行身份验证的请求。登录页面的 URL 是 "/login",登录成功后重定向到 "/home"。 在 configure 方法中,我们还使用了一个内存身份验证来验证用户。在这个示例中,我们创建了一个用户名为 "user",密码为 "password" 的用户,他拥有 "USER" 角色。 注意,这个示例只是一个基本的示例,你需要根据自己的实际需求进行配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值