SpringBoot项目访问任意接口都跳转到login登录页面

以下是我搜集到的三个解决方案,推荐第三种

有一段时间没用了SpringBoot,再次使用后发现SpringBoot项目启动后访问跟目录竟然跳转到login页面,而我本人未编写任何界面。

编写接口发现还是跳转到这个页面,之后查找资料,是导入的jar包中有一个登陆拦截器,这是看到Borvein的文章(https://blog.csdn.net/qq_36079461/article/details/96759099)有所了解

解决方案1:

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

去掉以上依赖就可以

之后重新创建了一个SpringBoot项目并且没有勾选Security 其中的选项后就没这个问题了

之后查阅whyalwaysmea的文章后https://blog.csdn.net/u013435893/article/details/79596628

大致能理解加入了安全认证机制所以会跳转到登录页面

解决方案2:

spring boot1.5配置security关闭http基本验证,只需要在application.properites中配置
security.basic.enabled=false即可,
但是spring boot 2.0+之后这样配置就不能生效了。

参考了 yinzhenghao1的文章https://blog.csdn.net/qq_42703181/article/details/86361078

解决方案3:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //super.configure(http);
        //配置不需要登陆验证
        http.authorizeRequests().anyRequest().permitAll().and().logout().permitAll();
    }

}

 

 

  • 16
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
你可以通过配置 Spring Boot 的视图解析器和控制器来实现在访问默认端口时跳转到 index 页面。 1. 配置视图解析器 在 Spring Boot 项目中,你可以通过在 application.properties 或 application.yml 文件中添加以下配置来配置视图解析器: application.properties: ``` spring.mvc.view.prefix=/WEB-INF/views/ spring.mvc.view.suffix=.jsp ``` application.yml: ``` spring: mvc: view: prefix: /WEB-INF/views/ suffix: .jsp ``` 这里我们使用 JSP 页面作为示例,你也可以使用其他类型的视图。 2. 创建控制器 在 Spring Boot 中,你可以使用 @Controller 注解来声明一个控制器。在控制器中,你可以使用 @RequestMapping 注解来指定请求的 URL 和 HTTP 方法。 在我们的示例中,我们需要创建一个控制器来处理根路径的请求。在控制器中,我们可以使用 @RequestMapping("/") 注解来指定根路径的请求。我们还可以使用 @GetMapping 注解来指定 GET 请求。在控制器的方法中,我们可以使用 ModelAndView 对象来指定要返回的视图和模型数据。 下面是一个示例控制器的代码: ```java @Controller public class HomeController { @GetMapping("/") public ModelAndView home() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("index"); return modelAndView; } } ``` 这个控制器会处理根路径的 GET 请求,返回名为 "index" 的 JSP 页面。 3. 配置默认端口 在 Spring Boot 中,默认的端口号是 8080。如果你想要更改默认端口号,可以在 application.properties 或 application.yml 文件中添加以下配置: application.properties: ``` server.port=8081 ``` application.yml: ``` server: port: 8081 ``` 这里我们将端口号设置为 8081。 现在,当你访问 http://localhost:8081/ 时,就会自动跳转到 index 页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值