美文网首页
springBoot登入注册(登入拦截器)

springBoot登入注册(登入拦截器)

作者: Aluha_f289 | 来源:发表于2019-06-24 20:00 被阅读0次

登入controller

 /**

     * POST /login  登入验证

     * @param map: 登入验证信息

     *           userName: 登入人姓名

     *           password: 登入人密码

     * @param session 登入信息正确把登入人信息放在session中

     * @return result

     *             YES 登入成功

     */

    @PostMapping("/login")

    public Map<String, String> login (@RequestBody Map<String, String> map, HttpSession session) {

        String userName = map.get("userName");

        String password = map.get("password");

        Map<String,String> mapResult = null;

        try {

            mapResult = loginService.isExistUser(userName,password);

            if (mapResult.get("result").equals("YES")) {

                session.setAttribute("userName",userName);

            }

        }catch (Exception e) {

            e.printStackTrace();

        }

        //返回验证结果

        return mapResult;

    }

实现类

/**

     * @param userName: 登入人姓名

     * @param password: 登入人密码

     * @return YES 登入成功

     */

    @Override

    public Map<String, String> isExistUser(String userName, String password) {

        //判断用户是否存在

        User u = userRepository.findOneByUserName(userName);

        Map<String, String> map = new HashMap<>();



        //如果用户存在判断用户的密码是否正确

        if (u == null) {

            map.put("result","用户不存在!");

        }else if (u != null && password.equals(u.getPassword())) {

            map.put("result","YES");



        }else {

            map.put("result","用户密码错误!");

        }



        return map;

    }

拦截器

 /**

     * 这个方法是在访问接口之前执行的,我们只需要在这里写验证登陆状态的业务逻辑,就可以在用户调用指定接口之前验证登陆状态了

     * @param request

     * @param response

     * @param handler

     * @return

     * @throws Exception

     */

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

        //每一个项目对于登陆的实现逻辑都有所区别,我这里使用最简单的Session提取User来验证登陆。

        HttpSession session = request.getSession();

        //这里的User是登陆时放入session的

        Object userName =  session.getAttribute("userName");

        //如果session中没有user,表示没登陆

        if (userName == null){

        // 这个方法返回false表示忽略当前请求,如果一个用户调用了需要登陆才能使用的接口,如果他没有登陆这里会直接忽略掉

            return false;

         }else {

           return true;

           }

     }

properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.datasource.tomcat.max-wait = 10000
spring.datasource.tomcat.max-active = 50
spring.datasource.tomcat.test-on-borrow = true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true



#thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false

logging.level.com.lxj.cache.mappers=debug

gitHub地址

https://github.com/baoqintian/springBootDemo欢迎大家来fork

相关文章

网友评论

      本文标题:springBoot登入注册(登入拦截器)

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