当前位置: 移动技术网 > IT编程>开发语言>Java > Spring项目整合成SpringBoot的简单登录拦截Demo

Spring项目整合成SpringBoot的简单登录拦截Demo

2020年08月17日  | 移动技术网IT编程  | 我要评论
sss整合成SpringBoot环境:​jdk11​tomcat 8.5.41​springboot 2.3.2.RELEASE​redisjar包和war包 启动jar包:SpringBoot本身不支持JSPhttps://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-jsp-limitations如何让SpringBoot的jar包兼容jsp?spring-boo

sss整合成SpringBoot

  • 环境:

    jdk11

    tomcat 8.5.41

    springboot 2.3.2.RELEASE

    redis

  • jar包和war包 启动

    jar包:SpringBoot本身不支持JSP

    https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-jsp-limitations

    如何让SpringBoot的jar包兼容jsp?

    spring-boot-starter-web中有tomcat.embed.core等依赖,但是没有tomcat-embed-jasper依赖,因此需要在pom中额外添加,用来解析jsp,否则打开jsp会直接下载文件。

    此处并不像网上文章所说需要添加<scope>provided</scope>,实测添不添加,用main或者jar启动都可以正常访问,没有报错也没有冲突。照理说如果添加了反而不会打到jar包中应该会无效,这里求高人指点。

    <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <!-- <scope>provided</scope> --> </dependency> 如果用到servlet或者jstl则添加相关依赖,没有用到可以不加。 

    此时直接main方法启动是可以正常访问的,但是打包会发现没有把webapp中的内容打到jar中,因此pom中还需要在plugins后面添加一段

    <resources> <resource> <directory>src/main/webapp</directory> <targetPath>META-INF/resources</targetPath> </resource> </resources> 

    此时会发现jar包webapp下的jsp已经被打到META-INF/resources下了,但是仍然访问不到,经过查询还需要springbootmaven1.4.2及以下的版本才可以

    <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.4.2.RELEASE</version> </plugin> </plugins> 

    由于我在static中加了一个jsp作测试,这里也必须配置resource标签,否则将无法访问到static中的jsp,只能访问静态资源。这里不建议这么做,因为这样的话resources目录中的文件会被打包两次,分别放在BOOT-INF和META-INF中。

     <resource> <!-- 指定resources插件处理哪个目录下的资源文件 --> <directory>src/main/resources/static</directory> <targetPath>META-INF/resources</targetPath> </resource> 

    到此打包完成,jar运行成功访问到webapp下的jsp

    https://blog.csdn.net/ssxueyi/article/details/100990002

    war包:
    不需要tomcat-embed-jasper
    也不需要spring-boot-starter-tomcat
    也不需要javax.servlet
    不知道为什么网上说需要排除自身的tomcat否则会冲突,实测不会
    不需要下面的这些 <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> 只需启动类改为:
    @SpringBootApplication
    public class BootwebclusterApplication extends SpringBootServletInitializer {
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(BootwebclusterApplication.class);
        }
        public static void main(String[] args) {
    		SpringApplication.run(BootwebclusterApplication.class, args);
        }
    }
    并且配置<packaging>war</packaging> 
  • jar包改war后如果不是在root中运行,则需要在跳转链接前面加项目名

    java:
    	request.getContextPath()
    jsp:
    	<%= this.getServletContext().getContextPath() %>
    el:
    	${pageContext.request.contextPath} 
  • 拦截器

    会拦截静态资源。

    如果访问不存在的资源会跳转到/error,但error仍会被拦截,因此会被跳转到登录界面,因此拦截器配置如下

    @Configuration public class MvcConfig implements WebMvcConfigurer { //  注入拦截器 @Bean public LoginInteceptor getLoginInterceptor(){ return new LoginInteceptor(); } //   把自定义的拦截器添加到mvc 配置中 @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(this.getLoginInterceptor()) .addPathPatterns("/**").excludePathPatterns("/login","/error" ,"/**/*.jsp" ,"/**/*.js","/**/*.css","/**/font/*"); } } 
  • ajax请求到后台重定向或设置视图无效

demo地址
https://gitee.com/NathanWH/Learn/tree/master/%E9%98%B6%E6%AE%B5%E4%BA%8C/web_cluster/bootwebcluster

本文地址:https://blog.csdn.net/NNNathan/article/details/108017760

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网