Springboot实现拦截器Demo

Springboot实现拦截器Demo

初学者写的小demo,如有错误,请多多指正!

配置文件

spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root

mybatis.mapper-locations=classpath:/com.eduai.mapper/*.xml

Pom.xml文件中的依赖jar

<properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.3version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.48version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

xml映射文件



<mapper namespace="com.eduai.mapper.UserLoginMapper">
    <select id="selectOneUser" parameterType="com.eduai.pojo.People" resultType="com.eduai.pojo.People">
        select * from userinfo where username = #{username} and password = #{password}
    select>
mapper>

接口Mapper

@Mapper
public interface UserLoginMapper {

    People selectOneUser(People people);
}

接口Service

public interface UserLoginService {

    People findOneUser(People people);
}

接口实现ServiceImpl

@Service
public class UserLoginServiceImpl implements UserLoginService {

    @Resource
    private UserLoginMapper userLoginMapper;

    @Override
    public People findOneUser(People people) {
        return userLoginMapper.selectOneUser(people);
    }
}

Controller

@Controller
@RequestMapping(value = "/user")
public class UserLoginController {

    @Resource
    private UserLoginService userLoginService;

    @RequestMapping(value = "/login.do")
    public String userLogin(People people, HttpSession session){
        String username = people.getUsername();
        String password = people.getPassword();
        System.out.println(username+":"+password);

        people = userLoginService.findOneUser(people);
        if(people!=null){
            session.setAttribute("user",people);
            System.out.println(people);
            return "main";
        }
        return "error";
    }
}

LoginConfig类

@Configuration
public class LoginConfig implements WebMvcConfigurer {
    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration resgistration = registry.addInterceptor(new UserLoginInterceptor());
        resgistration.addPathPatterns("/**");
        resgistration.excludePathPatterns("/login","/user/login.do");
    }
}

UserLoginInterceptor类

public class UserLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        People user = (People) request.getSession().getAttribute("user");
        if(user!=null){
            return true;
        }
        response.sendRedirect("/login");
        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 {

    }
}

login.html


<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户登录title>
head>
<body>
<form th:action="@{/user/login.do}" method="post">
    用户名:<input type="text" name="username" placeholder="用户名" /><br>
    密  码:<input type="text" name="password" placeholder="密码" />br>
          <input type="submit" value="登录" />
form>
body>
html>

main.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主界面title>
head>
<body>
    <h2>Hello Worldh2>
body>
html>

error.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>错误页面title>
head>
<body>
<h3>sorry!not found pageh3>
body>
html>

你可能感兴趣的:(Springboot实现拦截器Demo)