Shiro是一个用于身份验证、授权和会话管理的Java安全框架。它提供了一套易于使用的API,可以帮助开发人员构建安全性强大的应用程序。
- <dependency>
- <groupId>org.apache.shiro</groupId>
- <artifactId>shiro-core</artifactId>
- <version>1.9.0</version>
- </dependency>
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.2</version>
- </dependency>
- [users]
- zhangsan=z3
- lisi=l4
登录认证实例- public class ShiroRun {
- public static void main(String[] args) {
- IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
- SecurityManager securityManager = (SecurityManager) factory.getInstance();
- SecurityUtils.setSecurityManager((SecurityManager) securityManager);
- //2 获取 Subject 对象
- Subject subject = SecurityUtils.getSubject();
- //3 创建 token 对象,web 应用用户名密码从页面传递
- AuthenticationToken token = new UsernamePasswordToken("zhangsan","z3");
- //4 完成登录
- try {
- subject.login(token);
- System.out.println("登录成功");
- }
- catch (UnknownAccountException e) {
- e.printStackTrace();
- System.out.println("用户不存在");
- }
- catch (IncorrectCredentialsException e) {
- e.printStackTrace();
- System.out.println("密码错误");
- }
- catch (AuthenticationException ae) {
- //unexpected condition? error?
- }
- }
- }
角色授权 - if (subject.hasRole("admin")) {
- // 有权限
- }else {
- // 无权限
- }
- @RequiresRoles("admin")
- public void hello(){
- // 有权限
- }
JSP/GSP 标签:在JSP/GSP 页面通过相应的标签完成

授权实例 - // 给shiro.ini增加角色配置
- [users]
- zhangsan=z3,role1,role2
- lisi=l4
- try {
- subject.login(token);
- System.out.println("登录成功");
- // 判断角色
- boolean role1 = subject.hasRole("role1");
- System.out.println("师范拥有此角色" + role1);
- }
- // 给shiro.ini增加权限配置
- [roles]
- role1=user:insert,user:select
- //判断权限
- boolean isPermitted = subject.isPermitted("user:insert");
- System.out.println("是否拥有此权限:"+isPermitted);
- //也可以用 checkPermission 方法,但没有返回值,没权限抛 AuthenticationException
- subject.checkPermission("user:select");

使用Shiro进行密码加密
- import org.apache.shiro.crypto.hash.Md5Hash;
- import org.apache.shiro.crypto.hash.SimpleHash;
-
- public class ShiroMD {
- public static void main(String[] args) {
- //密码明文
- String password = "z3";
- //使用 md5 加密
- Md5Hash md5Hash = new Md5Hash(password);
- System.out.println("md5 加密:"+md5Hash.toHex());
- //带盐的 md5 加密,盐就是在密码明文后拼接新字符串,然后再进行加密
- Md5Hash md5Hash2 = new Md5Hash(password,"salt");
- System.out.println("md5 带盐加密:"+md5Hash2.toHex());
- //为了保证安全,避免被破解还可以多次迭代加密,保证数据安全
- Md5Hash md5Hash3 = new Md5Hash(password,"salt",3);
- System.out.println("md5 带盐三次加密:"+md5Hash3.toHex());
- //使用父类实现加密
- SimpleHash simpleHash = new SimpleHash("MD5",password,"salt",3);
- System.out.println("父类带盐三次加密:"+simpleHash.toHex());
- }
- }
- public class MyRealm extends AuthenticatingRealm {
- //自定义的登录认证方法,Shiro 的 login 方法底层会调用该类的认证方法完成登录认证
- //需要配置自定义的 realm 生效,在 ini 文件中配置,或 Springboot 中配置
- //该方法只是获取进行对比的信息,认证逻辑还是按照 Shiro 的底层认证逻辑完成认证
- protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
- //1 获取身份信息
- String principal = authenticationToken.getPrincipal().toString();
- //2 获取凭证信息
- String password = new String((char[])
- authenticationToken.getCredentials());
- System.out.println("认证用户信息:"+principal+"---"+password);
- //3 获取数据库中存储的用户信息
- if(principal.equals("zhangsan")){
- //3.1 数据库存储的加盐迭代 3 次密码
- String pwdInfo = "7174f64b13022acd3c56e2781e098a5f";
- //3.2 创建封装了校验逻辑的对象,将要比较的数据给该对象
- AuthenticationInfo info = new SimpleAuthenticationInfo(
- authenticationToken.getPrincipal(),
- pwdInfo,
- ByteSource.Util.bytes("salt"),
- authenticationToken.getPrincipal().toString());
- return info;
- }
- return null;
- }
- }
- [main]
- md5CredentialsMatcher=org.apache.shiro.authc.credential.Md5CredentialsMatcher
- md5CredentialsMatcher.hashIterations=3
- myrealm=com.example.demo.component.MyRealm
- myrealm.credentialsMatcher=$md5CredentialsMatcher
- securityManager.realms=$realm
- [users]
- zhangsan=7174f64b13022acd3c56e2781e098a5f,role1,
- role2
- lisi=l4
- [roles]
- role1=user:insert,user:select
参数:
md5CredentialsMatcher=org.apache.shiro.authc.credential.Md5CredentialsMatcher:这行配置指定了使用MD5算法进行凭证匹配的CredentialsMatcher实现类。
md5CredentialsMatcher.hashIterations=3:这行配置指定了MD5算法的哈希迭代次数,即将密码哈希化的循环次数。
myrealm=com.example.demo.component.MyRealm:这行配置指定了自定义的Realm实现类,即MyRealm。
myrealm.credentialsMatcher=$md5CredentialsMatcher:这行配置将之前定义的md5CredentialsMatcher赋值给MyRealm的凭证匹配器(credentialsMatcher)属性。
securityManager.realms=$myrealm:这行配置将MyRealm添加到SecurityManager中的Realms列表中。
