package com.system.controller;
import com.system.exception.CustomException;
import com.system.po.Userlogin;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Created by 李杨勇.
*/
@Controller
public class LoginController {
//登录跳转
@RequestMapping(value = "/login", method = {RequestMethod.GET})
public String loginUI() throws Exception {
return "../../login";
}
//登录表单处理
@RequestMapping(value = "/login", method = {RequestMethod.POST})
public String login(Userlogin userlogin) throws Exception {
//Shiro实现登录
UsernamePasswordToken token = new UsernamePasswordToken(userlogin.getUsername(),
userlogin.getPassword());
Subject subject = SecurityUtils.getSubject();
//如果获取不到用户名就是登录失败,但登录失败的话,会直接抛出异常
subject.login(token);
if (subject.hasRole("admin")&userlogin.getRole()==0) {
return "redirect:/admin/showStudent";
} else if (subject.hasRole("teacher")&userlogin.getRole()==1) {
return "redirect:/teacher/showCourse";
} else if (subject.hasRole("student")&userlogin.getRole()==2) {
return "redirect:/student/showCourse";
}else throw new CustomException("请选择正确的身份登陆");
}
}
文件上传
package com.system.controller;
import com.system.po.FileVO;
import com.system.service.FileService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.UUID;
/**
* 文件上传下载
*/
@Controller
@RequestMapping("/file")
public class FileController {
@Resource(name = "fileServiceImpl")
private FileService fileService;
@RequestMapping("/upload")
public String fileUpload(@RequestParam MultipartFile file, FileVO filevo, HttpServletRequest request) throws IOException {
//上传路径保存设置
// 把文件写到磁盘
String fileName = file.getOriginalFilename();
String[] str = fileName.split("\\.");
String uuid = UUID.randomUUID().toString().replaceAll("-","");
String headPath = "E://upload/" + uuid+ "."+str[str.length-1];
File dest = new File(headPath);
file.transferTo(dest);
filevo.setFileID(uuid);
filevo.setFilePath(headPath);
filevo.setUserID(null);
try {
fileService.save(filevo);
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/admin/showFile";
}
@RequestMapping("/downFile")
public void down(HttpServletRequest request, HttpServletResponse response,String fileID) throws Exception{
FileVO fileVO = fileService.findById(fileID);
String fileName = fileVO.getFilePath();
String[] str = fileName.split("\\.");
InputStream bis