首页>代码>SpringBoot项目,tk.mybatis通用mapper支持,shiro权限管理,以及mybatis分页插件,文件上传实例>/springboot/src/main/java/com/test/springboot/controller/UsersController.java
package com.test.springboot.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.springboot.pojo.Users;
import com.test.springboot.service.UsersService;
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.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.naming.ldap.PagedResultsControl;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class UsersController {
@Autowired
private UsersService usersService;
//跳转登录页
@RequestMapping("/login")
public String getAllusers(){
return "login";
}
//未授权
@RequestMapping("/401")
public String weishouq(){
return "401";
}
@RequestMapping("/system")
public String page1(){
return "page1";
}
@RequestMapping("/public")
public String page2(){
return "page1";
}
@RequestMapping("/test")
@ResponseBody
public Map<String,Object> test(){
Map<String,Object> resultmap = new HashMap<String,Object>();
//List<Users> alluser = usersService.getAllUsers();
resultmap.put("userslist","1");
return resultmap;
}
@RequestMapping("/test2")
public void test2(PrintWriter out){
//List<Users> alluser = usersService.getAllUsers();
out.print(1);
}
@RequestMapping("/indexpage")
public String indexpage(Integer pageNum,Model m){
if(pageNum==null||pageNum<=0) {
pageNum=1;
}
PageInfo<Users> po = usersService.getallusers(pageNum);
System.out.println("+数据+"+po);
//当前页数
System.out.println("+当前页数+"+po.getPageNum());
//总页数
System.out.println("+总页数+"+po.getPages());
//总记录数
System.out.println("+总记录数+"+po.getTotal());
//设置当前页的列表内容
System.out.println("+设置当前页的列表内容+"+po.getList());
m.addAttribute("list", po.getList());
m.addAttribute("pageNum", po.getPageNum());
m.addAttribute("pagetotal", po.getPages());
return "index";
}
@RequestMapping("/doLogin")
public String doLogin(String username,String userpwd,Model m){
// 使用Shiro编写认证处理
// 1、获取Subject
Subject subject = SecurityUtils.getSubject();
// 2、封装用户数据
UsernamePasswordToken token = new UsernamePasswordToken(username, userpwd);
// 3、执行登录
try {
// 登录成功
subject.login(token);
// 返回当前用户的帐号
final Model currentuser = m.addAttribute("currentuser", token.getUsername());
indexpage(1,m);//调用分页方法
return "index";
} catch (UnknownAccountException exception) {
// 返回错误信息
m.addAttribute("msg", "账号错误!");
return "login";
} catch (IncorrectCredentialsException exception) {
// 返回错误信息
m.addAttribute("msg", "密码错误!");
return "login";
}
}
// 注销处理
@RequestMapping("/doLogout")
public String doLogout() {
// 1、获取Subject
Subject subject = SecurityUtils.getSubject();
// 2、执行注销
try {
subject.logout();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
return "login";
}
}
}
最近下载更多
微信网友_6377331253415936 LV3
2024年11月5日
微信网友_7134912998903808 LV15
2024年8月29日
1529860026 LV24
2022年9月5日
彬 LV6
2022年6月25日
wwssll LV2
2022年5月9日
heweimin LV13
2022年4月9日
szf123 LV12
2021年11月11日
qazxsw_ LV10
2021年9月17日
qq70081337 LV6
2021年9月10日
build12398 LV18
2021年7月28日
最近浏览更多
citybird LV4
2024年11月20日
微信网友_6377331253415936 LV3
2024年11月5日
bluerstar LV1
2024年10月23日
微信网友_7134912998903808 LV15
2024年8月29日
ma406805131 LV19
2024年6月15日
lllajen
2024年6月9日
暂无贡献等级
WBelong LV8
2024年3月7日
janly989713 LV3
2024年3月4日
weilaizhisheng LV21
2023年12月26日
wnnmmb LV2
2023年11月12日

