首页>代码>阿前制作spring+spring mvc+mybatis+vue开发学生系统后台管理系统>/ssm-gbq-root/gbq-aqi/src/main/java/com/ssm/gbq/controller/LoginController.java
package com.ssm.gbq.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.gbq.model.Manager;

import gbq.ssm.utils.BusinessException;
import gbq.ssm.utils.ValidateCode;
/**
 * 登录Controller
 * @author 阿前
 * 2019年1月4日09:48:21
 */
@Controller
@RequestMapping("")
public class LoginController {
    @ResponseBody
    @RequestMapping(value = "/login.zhtml", method = RequestMethod.POST)
//    public HashMap<String, Object> loginVeri(@Valid LoginForm loginForm, BindingResult bindingResult,
    public HashMap<String, Object> loginVeri(Manager loginForm, BindingResult bindingResult,HttpServletRequest request,
            HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*"); 
        HashMap<String, Object> result = new HashMap<String, Object>();
        try {
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            System.out.println(session.getId());
            if (subject.isAuthenticated()) {
                result.put("token", session.getId());
                return result;
            }
            if (bindingResult.hasErrors()) {
                List<ObjectError> allErrors = bindingResult.getAllErrors();
                for (ObjectError error : allErrors) {
                    result.put("error", error.getDefaultMessage());
                }
                return result;
            }
            // managerService.getManagerByName(loginForm.getUsername());
            // 身份验证
            subject.login(new UsernamePasswordToken(loginForm.getUsername(), loginForm.getPassword()));
            result.put("token", session.getId());
        } catch (AuthenticationException e) {
            // 身份验证失败
            result.put("error", "用户名或密码错误 !");
            return result;
        } catch (BusinessException e) {
            result.put("error", e.getErrorCode());
            return result;
        }
        return result;
    }
    
    /** 
     * 响应验证码页面 
     * @return 
     */  
    @ResponseBody
    @RequestMapping(value="/validateCode", method = RequestMethod.GET)  
    public String validateCode(HttpServletRequest request,HttpServletResponse response) throws Exception{  
        // 设置响应的类型格式为图片格式  
        response.setContentType("image/jpeg");  
        //禁止图像缓存。  
        response.setHeader("Pragma", "no-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0);  

        HttpSession session = request.getSession();  

        ValidateCode vCode = new ValidateCode(120,40,5,100);  
        session.setAttribute("code", vCode.getCode());  
        vCode.write(response.getOutputStream());  
        return null;  
    } 
}
最近下载更多
五折也挺好的  LV13 2023年7月7日
yuanye111  LV2 2023年5月31日
yunYUN123  LV1 2023年5月18日
kk1031  LV8 2023年4月13日
aaaaaaa陈  LV1 2023年3月21日
pangxiangdong  LV4 2023年3月12日
wangyang520  LV6 2023年3月11日
3473084268  LV9 2022年12月9日
Linglingyu  LV2 2022年11月24日
admin1234545545  LV3 2022年11月13日
最近浏览更多
1271247100  LV9 11月22日
dddding yang  LV6 9月15日
甜心冰淇淋  LV4 6月17日
2我额wdas 6月16日
暂无贡献等级
yifeng868  LV9 6月10日
段池卿  LV5 5月23日
Yj123456k  LV2 5月11日
lilimyeclipse  LV6 5月11日
暂无贡献等级
xiaoaitx  LV8 1月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友