• springboot拦截器实现强制登录


    首先依赖没有特殊要求

    项目结构

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>springboot</artifactId>
            <groupId>com.baizhi</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>springbootfile</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.1.13.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <version>9.0.31</version>
            </dependency>
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.3.3</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version>2.1.13.RELEASE</version>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <!--启动插件-->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>2.1.3.RELEASE</version>
                    <configuration>
                        <fork>true</fork>
                        <jvmArguments>-Dfile.encoding=utf-8</jvmArguments>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    login.jsp

    <html>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <body>
    
    <form action="/springbootfile/login">
        账号: <input type="text" name="username" > <br>
        密码: <input type="password" name="password" > <br>
        <input type="submit" value="提交">
    </form>
    </body>
    </html>
    

     UserController

    package com.baihzhi.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.servlet.http.HttpSession;
    
    /**
     * @Package: com.baihzhi.controller
     * @ClassName: UserController
     * @Author: alex
     * @CreateTime: 2020/11/9 19:08
     * @Description:
     */
    @Controller
    public class UserController {
       @RequestMapping("/login")
       public String login(String username, String password, HttpSession session){
           if(username.equals("alex")&&password.equals("12345")){
               session.setAttribute("status","status");
               return "uploadsuccess";
           }
           System.out.println("1111");
           return "error";
       }
        @RequestMapping("/show")
        public String show(){
           return "uploadsuccess";
        }
    }

    (interceptor拦截器包)LoginInterceptor(拦截器)

    package com.baihzhi.interceptor;
    
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     * @Package: com.baihzhi.interceptor
     * @ClassName: LoginInterceptor
     * @Author: alex
     * @CreateTime: 2020/11/9 19:21
     * @Description:
     */
    @Component //此处注解一定要写,不然会被springboot忽略,此处注解相当于bean标签
    public class LoginInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            HttpSession session = request.getSession();
            String status =(String) session.getAttribute("status");
            System.out.println("22222");
            if(status!=null){
                System.out.println("33333");
                return true;
            }
            response.sendRedirect(request.getContextPath()+"/login.jsp");
            return false;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    
        }
    }

    springbootApplication(入口类)

    package com.baihzhi;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * @Package: com.baihzhi
     * @ClassName: springbootfileApplication
     * @Author: alex
     * @CreateTime: 2020/11/9 14:07
     * @Description:
     */
    
    @SpringBootApplication
    public class springbootfileApplication {
        public static void main(String[] args) {
            SpringApplication.run(springbootfileApplication.class,args);
        }
    }

    SpringMVCConfig(配置拦截范围,注意和入口类在同一级别的包里面,便于扫描)

    package com.baihzhi;
    
    //import com.baihzhi.interceptor.LoginInterceptor;
    //import org.springframework.beans.factory.annotation.Autowired;
    //import org.springframework.context.annotation.Configuration;
    //import org.springframework.web.servlet.HandlerInterceptor;
    //import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
    //import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    //import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    //
    ///**
    // * @Package: com.baihzhi
    // * @ClassName: SpringMVCConfig
    // * @Author: alex
    // * @CreateTime: 2020/11/9 19:32
    // * @Description:
    // */
    //@Configuration
    //public class SpringMVCConfig implements WebMvcConfigurer {
    //    @Autowired
    //    private HandlerInterceptor ls;
    //
    //    @Override
    //    public void addInterceptors(InterceptorRegistry registry) {
    //        InterceptorRegistration interceptor = registry.addInterceptor(ls);
    //        //设置拦截范围
    //        interceptor.addPathPatterns("/**");
    //        //排除拦截
    //        interceptor.excludePathPatterns("/login","/register");
    //    }
    //}
    
    import com.baihzhi.interceptor.LoginInterceptor;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class SpringMVCConfig implements WebMvcConfigurer {
        @Autowired
        private LoginInterceptor loginInterceptor;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            InterceptorRegistration ir = registry.addInterceptor(loginInterceptor);
            ir.addPathPatterns("/**");
            ir.excludePathPatterns("/login","/register");
    
        }
    }

     application.yml

    server:
      port: 8989
      servlet:
        context-path: /springbootfile
    
    spring:
      mvc:
        view:
          prefix: /
          suffix: .jsp
    以粮为纲全面发展
  • 相关阅读:
    【LeetCode每日一题】2020.6.9 面试题46. 把数字翻译成字符串
    【NOIP2017提高A组模拟9.17】信仰是为了虚无之人
    【NOIP2017提高A组模拟9.17】猫
    【NOIP2017提高A组模拟9.17】组合数问题
    JZOJ 11.21 提高B组反思
    【NOIP2017提高A组模拟9.12】Arrays and Palindrome
    JZOJ【NOIP2013模拟联考14】隐藏指令
    JZOJ 11.14 提高B组反思
    CSP2020复赛游记
    JZOJ 【NOIP2017提高A组模拟9.14】捕老鼠
  • 原文地址:https://www.cnblogs.com/alexliuf/p/13950797.html
Copyright © 2020-2023  润新知