美文网首页测试知识整理合集Java相关
SpringBoot 项目访问接口跳转到默认登录页

SpringBoot 项目访问接口跳转到默认登录页

作者: 思念_似水流年 | 来源:发表于2021-10-22 11:09 被阅读0次

问题场景:

今天在新建一个 SpringBoot 项目时,在项目启动成功后,发现接口请求时跳转到了默认的 login 登录页面,而不是 Controller 中指定的跳转链接。


SpringBoot 默认登录页
接口无法正常返回数据

问题原因:

在 SpringBoot 项目中使用了 SpringSecurity,而 SpringSecurity 默认是生效的,此时接口都是被保护的,我们需要通过验证才能正常访问接口。 SpringSecurity 提供了一个默认的用户,用户名是 user,而密码则是启动项目时自动生成的。
可以看到,在项目启动的日志中,会打印出一段生成安全密码的日志:


自动生成的安全密码

大部分情况下,我们都不需要使用 SpringSecurity 默认的登录页面来限制接口的访问,那么要怎么修改配置来解决呢?

解决方案:

方案一:

在启动类中添加注解,去掉 SpringSecurity:

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
@MapperScan("com.sso.mbg.mapper")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

重新启动项目,再发送请求,发现可以正常返回接口数据:


接口正常返回数据

方案二:

在项目中添加一个配置类做处理(推荐使用

package com.sso.security;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * add by liunian
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

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

再次重启项目,调用接口,发现接口可以正常返回数据

适用方案:

一般选择第二种解决方案,新建一个 WebSecurityConfig 类继承 WebSecurityConfigurerAdapter,在 configure 方法中,指定过滤的规则,选择性地过滤掉部分不需要验证的接口。
如下所示:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().cors();
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        HttpMethod permitHttpMethod = HttpMethod.OPTIONS;
        String[] permitAntPatterns = new String[]{
                "/",
                "/user/login",
                "/" + frontend + "/**",
                "/springboot-admin/**"
        };

        http.authorizeRequests()
                .antMatchers(permitHttpMethod).permitAll()
                .antMatchers(permitAntPatterns).permitAll()
                .anyRequest().authenticated();

    }

相关文章

  • SpringBoot 项目访问接口跳转到默认登录页

    问题场景: 今天在新建一个 SpringBoot 项目时,在项目启动成功后,发现接口请求时跳转到了默认的 logi...

  • 【接口工具ApiPost】一个http请求例子(2)

    以登录功能接口为例 项目名称(默认) 目录名称(默认示例接口) 接口名称:登录功能 接口请求面板&响应面板 接口菜...

  • 关于禁止页面后退的一些方法

    最近在项目中遇到登录页面进行跳转到主页,退出登录跳转到登录界面等,发现浏览器默认的后退按钮很是蛋疼,因为项目上是尽...

  • SpringBoot 2.0设置默认访问页

    每个网站都需要一个默认访问的页面,方面用户访问,因此我们需要给我们的平台设置一个默认访问的URL。 我使用的是Sp...

  • OC-present到一个界面,在push下一界面

    在做项目的时候,登录的模块大概都是这种情况:点击登录present跳转到登录界面,点击注册或找回密码时 push跳...

  • SpringBoot项目修改访问端口和访问路径的方法

    创建SpringBoot项目,启动后,默认的访问路径即主机IP+默认端口号8080:http://localhos...

  • 忘记密码接口

    接口描述 忘记密码接口, 发送短信给用户,成功后跳转到登录页 请求方式只接受【POST】请求 请求参数说明 jso...

  • Locust性能测试(2)

    很多网站需要先登录才能访问其他的接口,下面以先登录然后访问->群管理页面->问题件管理页->索赔件管理为例 设置1...

  • 注册接口

    接口描述 注册接口, 成功后跳转到登录页 请求方式只接受【POST】请求 请求参数说明 json数据格式,具体内容...

  • 注销接口

    接口描述 注销接口, 成功后跳转到登录页 请求方式只接受【POST】请求 请求参数说明 json数据格式,具体内容...

网友评论

    本文标题:SpringBoot 项目访问接口跳转到默认登录页

    本文链接:https://www.haomeiwen.com/subject/mmucaltx.html