You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.9 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.emr.controller;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
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.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author HJL
* @create 2019/4/29
*/
@Controller
public class LoginController {
@RequestMapping(value = "/toLogin")
public String toLogin(Model model) {
return "Login";
}
//实现用户登录@PathVariable("username")
/*@ResponseBody*/
@RequestMapping(value = "/login")
public String Login(@RequestParam("username")String username, @RequestParam("password")String password, Model model) {
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
//token.setRememberMe(true);
Subject subject = SecurityUtils.getSubject();
String error = null;
try {
subject.login(token);
} catch (UnknownAccountException e) {
error = "用户名/密码错误";
} catch (IncorrectCredentialsException e) {
error = "用户名/密码错误";
} catch (AuthenticationException e) {
//其他错误比如锁定如果想单独处理请单独catch处理
error = "其他错误:" + e.getMessage();
}
System.out.println("error:" + error);
if (error != null) {//出错了,返回登录页面
model.addAttribute("error", error);
return "login";
} else {//登录成功
}
//登录成功后会跳转到successUrl配置的链接不用管下面返回的链接
return "index";
}
}