java 做登录跳转404_springboot 访问路径错误跳转到404(实现方法一)

本文介绍了如何在SpringBoot应用中实现登录后错误页面跳转到404页面。主要涉及创建ErrorController、获取工程中所有请求路径、自定义WebMvcInterceptor拦截器进行404处理,以及配置Interceptor排除静态资源路径。
摘要由CSDN通过智能技术生成

方法1:适用于POST请求,不适用于GET{}拼接参数

方法2:适用于模板,页面必须放到error文件夹下,不需要写任何java代码

方法3:适用于根据status值去判断,但是如果页面的图片地址是有动态参数,建议修改成相对引用,或者注入bean

目录

参考文章

前言

准备工作

404.html和ErrorCtrl

开始内部处理

所有访问

getPath()

applicationContext 之 ApplicationContextFactory

启动类SampleApplication

主要逻辑

404拦截处理WebMvcInterceptor

InterceptorConfig

效果图(略)

参考文章

获取本工程中有哪些请求路径

SpringBoot中使用ApplicationContext获取bean对象

SPRINGBOOT加入拦截器INTERCEPTOR

前言

有时候我们在登录网页的时候会发现,输入了错误的页面路径会变成如下页面:

05ac1ff1bb92214cfe81ae91d3b72f5e.png

实际上我们想要他能够跳转到404页面!那么这个需求该如何实现呢?

准备工作

404.html和ErrorCtrl

首先第一步我们先要画好404..html页面和对应的webctrl跳转做准备好:

import cloud.maque.biz.tdsc.config.properties.MaqueServiceProperties;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.Cookie;

@Controller

@RequestMapping("/error")

public class ErrorController {

@Autowired

MaqueServiceProperties maqueServiceProperties;

/*404 错误页面*/

@GetMapping("/404")

public String error404(Model model) {

model.addAttribute("subDomainUrl",maqueServiceProperties.getSubDomainUrl());

return "common/404";

}

}

开始内部处理

所有访问

getPath()

这里就比较重要了,我们需要当前访问的web浏览器路径和整个工程中的路径比对,那么前提要获取整个工程的web访问路径,如何获取呢?

参考文章:获取本工程中有哪些请求路径

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.method.HandlerMethod;

import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping;

import org.springframework.web.servlet.mvc.method.RequestMappingInfo;

import java.util.*;

@Controller

@RequestMapping(value = "/test")

public class SpringBootTest {

@Autowired

ApplicationContext applicationContext;

@RequestMapping(value = "/getPath")

public

@ResponseBody

List getPath() {

Listlist = new ArrayList<>();

AbstractHandlerMethodMappingobjHandlerMethodMapping =

(AbstractHandlerMethodMapping) applicationContext.getBean("requestMappingHandlerMapping");

MapmapRet = objHandlerMethodMapping.getHandlerMethods();

for (RequestMappingInfo requestMappingInfo : mapRet.keySet()) {

Set set = requestMappingInfo.getPatternsCondition().getPatterns();

Iterator iterator = set.iterator();

while (iterator.hasNext()) {

list.add(iterator.next().toString());

}

}

return list;

}

}

applicationContext 之 ApplicationContextFactory

问题随之而来,我要在内容中可以直接获取,那么就是需要一个applicationContext这个对象去手动调用,这个就用到了ApplicationContextFactory:

import org.springframework.context.ApplicationContext;

/**

* Created by xuhy on 2020/7/30.

* 手动获取applicationContext对象

*/

public class ApplicationContextFactory {

private static ApplicationContext applicationContext = null;

public static void setApplicationContext(ApplicationContext applicationContext) {

ApplicationContextFactory.applicationContext = applicationContext;

}

public static ApplicationContext getApplicationContext() {

return applicationContext;

}

}

启动类SampleApplication

然后在启动的时候放进去:

参考文章:SpringBoot中使用ApplicationContext获取bean对象

ConfigurableApplicationContext app = SpringApplication.run(SampleApplication.class, args);

ApplicationContextFactory.setApplicationContext(app);

b537de8e28ada02881380da33aecd8e6.png

主要逻辑

我们开始综上所述写404处理逻辑;

404拦截处理WebMvcInterceptor

这里包含了当前url和所有工程url比对:

import org.springframework.context.ApplicationContext;

import org.springframework.web.method.HandlerMethod;

import org.springframework.web.servlet.HandlerInterceptor;

import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping;

import org.springframework.web.servlet.mvc.method.RequestMappingInfo;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.util.*;

/**

* Created by xuhy on 2020/7/30.

* 拦截错误web请求

*/

public class WebMvcInterceptor implements HandlerInterceptor {

@Override

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {

// model.addAttribute("subDomainUrl",maqueServiceProperties.getSubDomainUrl());

// 获取所有请求路径

Listpaths = getPath();

// 匹配结果

Object[] result = paths.stream().filter(path -> path.equals(request.getRequestURI())).toArray();

if (result.length == 0) {

return true;

} else {

response.sendRedirect("/error/404");

return false;

}

}

/**

* 获取所有webe请求路径

* @return

*/

ListgetPath() {

Listlist = new ArrayList<>();

ApplicationContext applicationContext = ApplicationContextFactory.getApplicationContext();

AbstractHandlerMethodMappingobjHandlerMethodMapping =

(AbstractHandlerMethodMapping) applicationContext.getBean("requestMappingHandlerMapping");

MapmapRet = objHandlerMethodMapping.getHandlerMethods();

for (RequestMappingInfo requestMappingInfo : mapRet.keySet()) {

Set set = requestMappingInfo.getPatternsCondition().getPatterns();

Iterator iterator = set.iterator();

while (iterator.hasNext()) {

list.add(iterator.next().toString());

}

}

return list;

}

}

InterceptorConfig

/**

*  @description: web拦截器

*  @author xuhy

*  @date: 2020/7/31

*/

@Configuration

public class InterceptorConfig implements WebMvcConfigurer {

// 如果没写@Bean 则需要通过new xx写,写了则 registry.addInterceptor(webMvcInterceptor()).addPathPatterns("/**")

@Bean

public WebMvcInterceptor webMvcInterceptor() {

return new WebMvcInterceptor();

}

@Override

public void addInterceptors(InterceptorRegistry registry) {

//404.html blank.html 500.html 此方法添加拦截器

registry.addInterceptor(new WebMvcInterceptor()).addPathPatterns("/**");

}

}

值得注意的是

.excludePathPatterns("/js/**");

//404.html blank.html 500.html 此方法添加拦截器

registry.addInterceptor(new WebMvcInterceptor()).addPathPatterns("/**").

excludePathPatterns("/js/**", "/css/**", "/img/**","/api/**");

,不让然会出现图片访问正常的情况!!!

参考文章:SPRINGBOOT加入拦截器INTERCEPTOR

效果图(略)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以帮你。首先,你需要确保已经安装了Java和Node.js环境以及相关的开发工具。接下来,你可以按照以下步骤进行操作: 1. 创建一个Spring Boot项目: - 打开你的IDE(如IntelliJ IDEA)并创建一个新的Spring Boot项目。 - 选择Maven项目并添加Web和Security依赖。 2. 创建一个登录页面: - 在src/main/resources/static目录下创建一个名为login.html的文件。 - 在login.html文件中编写登录页面的HTML代码,包括表单和提交按钮。 3. 创建一个登录接口: - 在src/main/java目录下创建一个名为controller的包。 - 在controller包中创建一个名为LoginController的类。 - 在LoginController类中添加一个用于处理登录请求的方法,并使用@RequestMapping注解指定请求路径和请求方法。 - 在该方法中,获取用户提交的用户名和密码,并进行验证。 4. 创建一个Vue项目: - 打开终端,并进入到你想要创建Vue项目的目录中。 - 运行以下命令来创建一个名为frontend的Vue项目:`vue create frontend`。 - 根据提示选择默认配置或者自定义配置。 5. 修改Vue项目配置: - 进入frontend目录并打开src/main.js文件。 - 在main.js文件中添加以下代码来引入axios库:`import axios from 'axios'`。 - 在Vue实例(new Vue({...}))的created钩子函数中添加以下代码来发送登录请求:`axios.post('/login', {username: 'your_username', password: 'your_password'})`。 6. 启动应用: - 在IDE中运行Spring Boot项目。 - 进入frontend目录并运行`npm run serve`命令来启动Vue项目。 现在,你可以在浏览器中访问http://localhost:8080/login.html来查看登录页面,并尝试进行登录操作。请确保将"your_username"和"your_password"替换为实际的用户名和密码。 希望以上步骤对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值