SSM项目框架的学习
转载:https://blog.csdn.net/chijiandi/article/details/80169758 作者:从放弃到开始
目的:主要去看代码,理解代码
快速了解一个项目:https://www.cnblogs.com/orac/p/7852776.html 作者:改变
一、web.xml
shiro学习
参考文档:https://www.cnblogs.com/learnhow/p/5694876.html 作者:思踌之路
权限管理系统包括:认证和授权两部分
1. shiro简单实例
archetypeCatalog=internal maven项目的骨架不要到远程下载而是本地获取 // 貌似maven不在支持这个了
1) 环境搭建
2) shiro.ini
[users]
zhangsan=123456
sili=123456
了解shiro.ini文件 https://blog.csdn.net/u011781521/article/details/74892074 作者:lfendo
3) 简单认证
public class ShiroTest {
public static void main(String[] args) {
// 1. 获取实例,加载shiro-first.ini
IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro-first.ini");
// 2. 获取安全管理器securityManager
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
// 3. 将创建好的安全管理器注入到SecurityUtils中去
SecurityUtils.setSecurityManager(securityManager);
// 4. 获取主体(用户)
Subject subject = SecurityUtils.getSubject();
// 5. 模拟用户输入的信息
String userName = "zhangsan1";
String password = "123456";
// 6. 创建Token,通过UsernamePasswordToken包装客户端发送信息(账号,密码等信息)
UsernamePasswordToken token = new UsernamePasswordToken(userName,password);
try {
// 7. 进行登录和认证
subject.login(token);
} catch (AuthenticationException e) {
e.printStackTrace();
}
// 8. 是否已认证通过
boolean flag = subject.isAuthenticated();
System.out.println(flag);
}
}
2. shiro查询数据库
使用Realm与数据库进行交互 自定义realm
1) 环境配置
2) shiro-realm.ini
[main]
customRealm=com.yan.shiro.realm.CustomRealm
securityManager.realms=$customRealm
3) CustomRealm
package com.yan.shiro.realm;
import com.yan.shiro.domain.User;
import com.yan.shiro.service.IUserService;
import com.yan.shiro.service.impl.UserServiceImpl;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import java.util.Set;
public class CustomRealm extends AuthorizingRealm {
private IUserService userService = new UserServiceImpl();
@Override
public String getName() {
return "CustomRealm";
}
/**
* 进行授权操作
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("开始进行授权");
String userName = (String) principalCollection.getPrimaryPrincipal();
// 根据用户查询用户角色
Set<String> roleNameList = userService.findAllRoleNameByUserName(userName);
// 将用户角色信息交给shiro进行管理
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(roleNameList);
// 将用户操作[user:add user:delete role:add]
Set<String> permissionNames = userService.findAllPermissionNameByUserName(userName);
simpleAuthorizationInfo.addStringPermissions(permissionNames);
return simpleAuthorizationInfo;
}
/**
* 进行认证操作
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
System.out.println("开始进行认证");
// 通过token获取用户信息
String userName = (String) token.getPrincipal();
System.out.println(userName);
// 这个无法获取用户密码
// String password = (String) token.getCredentials();
// System.out.println("userName:" + userName + ",password:" + password);
// 根据用户的账号获取用户信息
User user = userService.findByName(userName);
if (user == null){
return null;
}
// 获取用户的密码
String p = user.getPassword();
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(userName,p,this.getName());
return simpleAuthenticationInfo;
}
}
3) 测试
package com.yan.shiro.service.impl;
import com.yan.shiro.domain.User;
import com.yan.shiro.service.IUserService;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class UserServiceImpl implements IUserService {
private static List<User> userList = new ArrayList<>();
static {
User user1 = new User();
user1.setId(1);
user1.setName("zhangsan");
user1.setPassword("123456");
userList.add(user1);
User user2 = new User();
user2.setId(2);
user2.setName("lisi");
user2.setPassw