关于 Apache Shiro 概念基本都粘自官网 http://shiro.apache.org/
Apache Shiro是一个功能强大且灵活的开源安全框架,可以清晰地处理身份验证,授权,企业会话管理和加密。
以下是Apache Shiro可以做的一些事情:
可以参考官方文档: http://shiro.apache.org/archi...
缓存架包先用 ehcache
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-all</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.6</version> </dependency>
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!-- mac 电脑, 跟 win 设置路径有点不一样 示例: path="d:/ehcache/" -->
<diskStore path="${user.home}/Downloads/ehcache" />
<!-- 默认缓存配置 没有特别指定就用这个配置 -->
<defaultCache maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU"
diskExpiryThreadIntervalSeconds="120" />
</ehcache>
<!-- 1. 配置 shiro 拦截器 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 2. 配置shiro的核心组件 SecurityManager -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 配置缓存 -->
<property name="cacheManager" ref="cacheManager" />
<property name="realm" ref="realm" />
</bean>
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"></property>
</bean>
<bean id="realm" class="com.ogemray.shiro.MyShiroRealm">
<property name="credentialsMatcher">
<bean class="com.ogemray.shiro.MyCredentialsMatcher"></bean>
</property>
</bean>
<!-- 3. 配置shiroFilter,配置shiro的一些基本规则信息,id必须和web.xml中配置的拦截器名字一样(DelegatingFilterProxy 通过名字去spring容器中找注入的拦截器) -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/login.html"/> <!-- loginUrl 表示登录页面地址 -->
<property name="successUrl" value="/admin/main.html"/> <!-- successUrl 表示登录成功后跳转的页面 -->
<property name="unauthorizedUrl" value="/unauthorized.html"></property> <!-- 配置没有授权跳转页面 -->
<property name="filterChainDefinitions">
<value>
<!--/logout.action=logout --> <!-- logout 表示登出, 清空session, 这里不需要了, 因为已经在登出对象的方法里手动清空了 [ SecurityUtils.getSubject().logout() ] -->
/admin/userlist*=roles[user]
/admin/adduser*=roles["user,admin"] <!-- 表示拥有 user 角色 并且 拥有 admin 角色 -->
/admin/editRPRelation*=roles[admin],perms[user:insert,user:update,user:select,user:delete]
/admin/editURRelation*=perms[user:select]
/admin/**=authc
/**=anon
</value>
</property>
</bean>
public class MyShiroRealm extends AuthorizingRealm {
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("********************* 进行登录认证 *********************");
String username = (String) authenticationToken.getPrincipal(); //获取提交的用户名
User user = userRepository.findByUsername(username);
if (user == null) throw new UnknownAccountException("用户不存在, 请先注册然后再来登录");
if (user.getState() == 1) throw new LockedAccountException("该用户已经被管理员禁用, 请换个账号登录");
//接下来进行密码的比对
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), getName());
return info;
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("********************* 进行授权认证 *********************");
User user = (User) principalCollection.asList().get(0);
Set<String> roles = new HashSet<>();
Set<String> permissions = new HashSet<>();
user.getRoles().forEach(role -> {
roles.add(role.getRoleName());
role.getPermissions().forEach(permission -> {
permissions.add(permission.getPermissionName());
});
});
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.setRoles(roles);
info.setStringPermissions(permissions);
return info;
}
@Autowired
private UserRepository userRepository;
}
前端密码加密规则:ciphertext_pwd = AES.encrypt(MD5(password))
后端解密密码规则:md5_password = AES.desEncrypt(ciphertext_pwd)
后端匹对密码规则:(md5_password + 用户名做盐值) 进行 1024 次 MD5 转换,然后与数据库取出密码做比对
public class MyCredentialsMatcher implements CredentialsMatcher {
@Override
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
String tokenCredentials = Arrays.toString((char[]) token.getCredentials()); //前端传过来的密码
String accountCredentials = (String) info.getCredentials(); //数据库查询到的密码
//首先对前端传过来的密码进行AES解密 -> 清空 session 里面的key
Session session = SecurityUtils.getSubject().getSession();
String key = (String) session.getAttribute("AESKey");
try {
tokenCredentials = AesEncryptUtil.desEncrypt(tokenCredentials, key, key);
} catch (Exception e) {
throw new IncorrectCredentialsException("可能受到重放攻击, AES 解密失败");
}
session.removeAttribute("AESKey");
//加密方式 待加密数据 加密盐值 加密次数
SimpleHash simpleHash = new SimpleHash("MD5", tokenCredentials, token.getPrincipal(), 1024);
return simpleHash.toString().equals(accountCredentials);
}
}
@Service("userService")
public class UserServiceImpl implements UserService {
@Override
public void registerUser(User user) {
if (userRepository.existsByUsername(user.getUsername())) {
throw new RuntimeException("用户名已经被注册, 请换个用户名");
}
user.setState((byte) 0);
//密码进行加密
SimpleHash simpleHash = new SimpleHash("MD5", user.getPassword(), user.getUsername(), 1024);
user.setPassword(simpleHash.toString());
userRepository.save(user);
}
@Override
public void login(User user) {
Subject currentUser = SecurityUtils.getSubject();
if (currentUser.isAuthenticated() == false) { //没有登录过需要进行登录验证
UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword(), false);
currentUser.login(token);
}
}
@Autowired
private UserRepository userRepository;
}