1 Log -shiro authentication
1.1 modify the database password - it is encrypted
We do not know the password database need to change
Ideas:
Overwrite save method
@Test
public void testMD5()throws Exception{
List<Employee> employees = employeeRepository.findAll();
for (Employee employee : employees) {
employee.setPassword(MD5Util.createMd5Str(employee.getUsername()));
employeeRepository.save(employee);
}
}
1.2 The new user password should be encrypted
@Override
public void save(Employee employee) {
if(employee.getId() == null){
//对员工设置加密之后的密码
employee.setPassword(MD5Util.createMd5Str(employee.getPassword()));
}
//重新保存到数据库 新增或者修改都要调用
super.save(employee);
}
1.3 Add the login page
(1) preparing a landing page login.jsp
(2) login.jsp page provides a form form
(3) submit the login form, call the following to submit
function submitForm(){
//提交表单到后台程序
$("#loginForm").form('submit',{
url:'/login',
success:function(result){
//返回json字符串 {success:true,msg:'xxx'}
//$ jquery里面对象
result = $.parseJSON(result);
if(result.success){
//跳转主页对外暴露资源名
location.href = "/main";
}else{
$.messager.alert("温馨提示", result.msg, "info");
}
}
})
}
(4) LoginController login authentication process
//get方式提交
/*当你访问当前服务器的资源的时候默认访问 对外的暴露资源 lojin 此时没有携带参数就是get提交
直接跳转到登录界面
*/
@RequestMapping(value="/login",method = RequestMethod.GET)
public String login(){
System.out.println("京来啦");
// 返回登陆界面
return "login";
}
// 进入登陆界面后输入用户名和密码再提交就是post提交
@RequestMapping(value = "/login",method = RequestMethod.POST)
@ResponseBody
public AjaxResult login(String username,String password){
//得到主体(用户名)
Subject subject = SecurityUtils.getSubject();
System.out.println(subject);
//判断主体是否认证过若是没有认证过则
if(!subject.isAuthenticated()){
//认证 用户名和密码的令牌
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
//调用认证方法 该方法会调用到myrealm里面
subject.login(token);
} catch (UnknownAccountException e) {
e.printStackTrace();
System.out.println("用户不存在");
return new AjaxResult("用户不存在");
} catch (IncorrectCredentialsException e) {
e.printStackTrace();
System.out.println("密码不正确");
return new AjaxResult("密码不正确");
}catch (AuthenticationException e) {
e.printStackTrace();
System.out.println("其他认证异常");
return new AjaxResult("其他认证异常");
}
}
return new AjaxResult();
}
1.4 logout function
(1) Main Menu after login to add a logout function
<div data-options="region:'north',split:true" style="height:120px;">
<h1 style="margin-bottom: 10px">源码时代智销系统</h1>
<div style="text-align:right;margin-right: 10px">
欢迎<font color="red"><shiro:principal property="username"/></font>进入
<a href="/logout">注销</a>
</div>
</div>
(2) providing a registration method in which LoginController
@RequestMapping("/logout")
public String logout(){
// 从前台传过来的获取主体
Subject subject = SecurityUtils.getSubject();
// System.out.println(subject);
// 注销方法shiro自动注销登录
subject.logout();
// 重定向到登录页面 对外的暴露资源名
return "redirect:/login";
}
Enter Log 1.5
In the event page bind a keyboard, if it is detected that enter (the Enter key) to submit the form
$(document.documentElement).on("keyup", function(event) {
//console.debug(event.keyCode);
//获取你再键盘上面点击了按键
var keyCode = event.keyCode;
console.debug(keyCode);
if(keyCode===13){
//注册键盘事件之后 当判断为回车键的时候(回车键就是编号为13)还是掉用点击事件的方法一样的
submitForm();
}
});
1.6 Login Problems expired
Reason: If we had written off the system, clicks inside other pages, there will be nested login page
Solution: Let the child window page address assigned to the top of the window address
if(top!=window){
//子窗口 把当前子窗口的路径 赋值顶层窗口的路径
top.location.href = window.location.href;
}
2 authority - Authorization (difficulty)
2.1 Why do you need authorization
Safety
2.2 authorization involves several objects
User: ordinary people
Role: The rights group
Permissions: The resource is locked
Resources: specific content
2.3 Object-Relational
Employee Role Permission
Users and roles - many relationship
Roles and permissions: many relationships
Authority and resources: 1 to 1
Creating Domain object --Employee Role Permission to configure the relationship configuration,
Write repository layer
Write service layer
Write controller layer
2.4 Roles page display
Note: The permissions string handling
function permissionsFormat(obj){
if(obj!=null){
var permissionStr = "";
//循环数组 0 1
for(var key in obj){
//取出每一个权限
var permission = obj[key];
if(key <= obj.length-2) {
//拼接成字符串 最终展示到页面 +=叼的很
permissionStr += permission.name + ",";
}else{
//若是最后一条数据就不需要加上逗号所以数组的长度-2
permissionStr += permission.name;
}
}
// 返回拼接后的字符串
return permissionStr;
}
}
Add Roles 2.5 interface
Ready dialog box
Ready form table
Prepare two tables
2.6 Role of the page to add functionality
Double-click the event table
allPermissionGrid.datagrid({
fit:true, //自适应div窗口
fixed:true,//固定列
fitColumns:true,//自适应列
url:"/permission/page",//查询所有的权限
rownumbers:"true" ,//显示行号
pagination:"true",//分页
//双击后添加权限
onDblClickRow:itsource.addPermission
});
Form add a row of data
userPermissionGrid.datagrid('appendRow',row);
To delete a row of data table
userPermissionGrid.datagrid('deleteRow',index);
Echo Operation
roleDialog.dialog('center').dialog('open');
//回显数据
roleForm.form('load',row);
//回显表格数据
var permssions = row.permissions;
//用户已选择权限的表格
// userPermissionGrid.datagrid('loadData',permssions);
var permissions = [];//准备一个临时的数组变量
$.extend(permissions,row.permissions);//把表格的权限数据复制到临时数组变量
userPermissionGrid.datagrid("loadData",permissions);
}else{
$.messager.alert('提示信息','请选中一条数据进行修改',"info");
}
Submit additional parameter to save time
onSubmit: function(param){
//param提交--表格里面的数据 --
//private List<Permission> permissions = new ArrayList<>();
//获取到表格里面的数据
var rows = userPermissionGrid.datagrid('getRows');
for(var i=0;i<rows.length;i++){
var rowObj = rows[i];
param["permissions["+i+"].id"]=rowObj.id;
}
//重复密码验证 比较两个值是否相等 如果不相等 return false
//表示验证通过才能提交
return roleForm.form("validate");
}
Solve the problem of n-to-n
role.getPermissions().clear();