package com.terra.land.controller;
|
|
import com.terra.land.entity.LoginInfo;
|
import com.terra.land.entity.Result;
|
import com.terra.land.entity.User;
|
import com.terra.land.service.UserService;
|
import org.apache.shiro.SecurityUtils;
|
import org.apache.shiro.authc.*;
|
import org.apache.shiro.subject.Subject;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.servlet.ModelAndView;
|
|
/**
|
* LoginController
|
* @author
|
*/
|
@RestController
|
@RequestMapping("/")
|
public class ApiController {
|
@Autowired
|
UserService userService;
|
|
@RequestMapping("/getName")
|
public User getName(String loginName) {
|
return userService.queryUserByName(loginName);
|
}
|
|
/**
|
* 跳到首页
|
*
|
* @return String
|
*/
|
@RequestMapping({"/", "/toIndex"})
|
public ModelAndView toIndex(ModelAndView mv) {
|
mv.setViewName("index");
|
|
User user = (User) SecurityUtils.getSubject().getPrincipal();
|
if (user != null) {
|
mv.addObject("msg", "Hello " + user.getLoginName() + " !");
|
}
|
|
return mv;
|
}
|
|
/**
|
* 跳到登录页
|
*
|
* @return String
|
*/
|
@RequestMapping("/toLogin")
|
public ModelAndView toLogin(ModelAndView mv) {
|
mv.setViewName("login");
|
|
return mv;
|
}
|
|
/**
|
* 跳到未授权页面
|
*
|
* @return String
|
*/
|
@RequestMapping("/noauth")
|
public ModelAndView toNoAuth(ModelAndView mv) {
|
mv.setViewName("noauth");
|
|
return mv;
|
}
|
|
/**
|
* 用于测试记住我和认证的区别
|
*
|
* @return String
|
*/
|
@RequestMapping("/buy")
|
public String buy() {
|
Subject subject = SecurityUtils.getSubject();
|
|
// 只有认证后才能访问,如果只是记住我则需要先登录
|
if (!subject.isAuthenticated()) {
|
return "redirect:/toLogin";
|
}
|
|
return "add";
|
}
|
|
/**
|
* 登录认证
|
*
|
* @return String
|
*/
|
@RequestMapping("/login")
|
public ModelAndView login(String username, String password, String service, Integer rememberMe) {
|
ModelAndView mv = new ModelAndView();
|
try {
|
Subject subject = SecurityUtils.getSubject();
|
|
// 获取令牌
|
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
|
if (rememberMe != null && rememberMe == 1) {
|
token.setRememberMe(true);
|
}
|
|
// 登录认证
|
subject.login(token);
|
|
mv.setViewName("index");
|
if (service != null && service.length() > 0) {
|
mv.addObject("url", service);
|
} else {
|
mv.addObject("msg", username);
|
}
|
} catch (UnknownAccountException e) { // 返回null就会进入这里
|
mv.setViewName("login");
|
mv.addObject("msg", "用户名不存在!");
|
} catch (IncorrectCredentialsException e) { // 密码错误就会进入这里
|
mv.setViewName("login");
|
mv.addObject("msg", "密码错误!");
|
}
|
|
return mv;
|
}
|
|
/**
|
* 注销
|
*
|
* @return String
|
*/
|
@RequestMapping("/logout")
|
public ModelAndView logout() {
|
Subject subject = SecurityUtils.getSubject();
|
subject.logout();
|
|
ModelAndView mv = new ModelAndView();
|
mv.setViewName("login");
|
|
return mv;
|
}
|
|
/**
|
* Ajax登录
|
*
|
* @param userInfo
|
* @return
|
*/
|
@ResponseBody
|
@RequestMapping(value = "/ajaxLogin", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
|
public Result ajaxLogin(@RequestBody User userInfo) {
|
try {
|
Subject subject = SecurityUtils.getSubject();
|
|
UsernamePasswordToken token = new UsernamePasswordToken(userInfo.getLoginName(), userInfo.getPassword());
|
subject.login(token);
|
|
LoginInfo loginInfo = userService.getLoginInfo(userInfo.getLoginName());
|
|
return new Result(200, loginInfo != null ? "登录成功" : "登录失败", loginInfo);
|
} catch (IncorrectCredentialsException e) {
|
return new Result(500, "密码错误");
|
} catch (LockedAccountException e) {
|
return new Result(500, "登录失败,该用户已被冻结");
|
} catch (AuthenticationException e) {
|
return new Result(500, "该用户不存在");
|
} catch (Exception e) {
|
e.printStackTrace();
|
return new Result(500, e.getMessage());
|
}
|
}
|
}
|