content: package com.gm.wj.controller;
  
  import com.gm.wj.entity.User;
  import com.gm.wj.result.Result;
  import com.gm.wj.result.ResultFactory;
  import com.gm.wj.service.UserService;
  import org.apache.shiro.SecurityUtils;
  import org.apache.shiro.authc.IncorrectCredentialsException;
  import org.apache.shiro.authc.UnknownAccountException;
  import org.apache.shiro.authc.UsernamePasswordToken;
  import org.apache.shiro.subject.Subject;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.*;
  import org.springframework.web.util.HtmlUtils;
  
  import javax.validation.Valid;
  
  /**
   * Login and register controller.
   *
   * @author Evan
   * @date 2019/4
   */
  @RestController
  public class LoginController {
  
      @Autowired
      UserService userService;
  
      @PostMapping("/api/login")
      public Result login(@RequestBody User requestUser) {
          String username = requestUser.getUsername();
          username = HtmlUtils.htmlEscape(username);
  
          Subject subject = SecurityUtils.getSubject();
  //        subject.getSession().setTimeout(10000);
          UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, requestUser.getPassword());
          usernamePasswordToken.setRememberMe(true);
          try {
              subject.login(usernamePasswordToken);
              User user = userService.findByUsername(username);
              if (!user.isEnabled()) {
                  return ResultFactory.buildFailResult("该用户已被禁用");
              }
              return ResultFactory.buildSuccessResult(username);
          } catch (IncorrectCredentialsException e) {
              return ResultFactory.buildFailResult("密码错误");
          } catch (UnknownAccountException e) {
              return ResultFactory.buildFailResult("账号不存在");
          }
      }
  
      @PostMapping("/api/register")
      public Result register(@RequestBody User user) {
          int status = userService.register(user);
          switch (status) {
              case 0:
                  return ResultFactory.buildFailResult("用户名和密码不能为空");
              case 1:
                  return ResultFactory.buildSuccessResult("注册成功");
              case 2:
                  return ResultFactory.buildFailResult("用户已存在");
          }
          return ResultFactory.buildFailResult("未知错误");
      }
  
      @GetMapping("/api/logout")
      public Result logout() {
          Subject subject = SecurityUtils.getSubject();
          subject.logout();
          return ResultFactory.buildSuccessResult("成功登出");
      }
  
      @GetMapping("/api/authentication")
      public String authentication() {
          return "身份认证成功";
      }
  }
  