shiro入门级小程序
2017-11-02 22:44
435 查看
这是使用的maven项目完成的一个shiro的小程序,注释非常详细,具体主要代码如下:
如果需要完成整的代码请到以下网址进行下载: https://github.com/itzzuli/shiro-base.git
package shiro_1.shiro_1; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.IncorrectCredentialsException; import org.apache.shiro.authc.LockedAccountException; import org.apache.shiro.authc.UnknownAccountException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.config.IniSecurityManagerFactory; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.session.Session; import org.apache.shiro.subject.Subject; import org.apache.shiro.util.Factory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Hello world! * */ public class App { static Logger logger = LoggerFactory.getLogger(App.class); public static void main(String[] args) { Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); Subject currentUser = SecurityUtils.getSubject(); Session session = currentUser.getSession(); session.setAttribute("someKey", "aValue"); String value = (String) session.getAttribute("someKey"); if (value.equals("aValue")) {// 获取session logger.info("================== Retrieved the correct value! " + value); } // 测试当前的用户是否已经被认证,即是否已经登录 // 调动Subject的isAuthenticated() if (!currentUser.isAuthenticated()) { // 将用户名和密码封装到UsernamePasswordToken对象当中 UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa"); token.setRememberMe(true); // 执行登录 try { currentUser.login(token); } // 若没有指定的账户, 则 shiro 将会抛出 UnknownAccountException 异常. catch (UnknownAccountException uae) { logger.info("================== There is no user with username of " + token.getPrincipal()); return; } // 若账户存在, 但密码不匹配, 则 shiro 会抛出 IncorrectCredentialsException 异常。 catch (IncorrectCredentialsException ice) { logger.info("================== Password for account " + token.getPrincipal() + " was incorrect!"); return; } // 用户被锁定的异常 LockedAccountException catch (LockedAccountException lae) { logger.info("================== The account for username " + token.getPrincipal() + " is locked. " + "Please contact your administrator to unlock it."); } // 所有认证时异常的父类. catch (AuthenticationException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } logger.info("================== User is " + currentUser.getPrincipal() + " logged is Successfully."); // 测试是否某一个角色。调用Subject的hasRole方法 if (currentUser.hasRole("schwartz")) { logger.info("================== May the Schwartz be with you!"); } else { logger.info("================== Hello,mere mortal."); return; } // 测试用户是否具备某一个行为,调用Subject的isPermitted()方法 if (currentUser.isPermitted("lightsaber:weild")) { logger.info("================== You may use a lightsaber ring.Use it wisely"); } else { logger.info("================== Sorry,lightsaber rings are for schwartz master only."); } // 测试用户是否具备某一个行为。 if (currentUser.isPermitted("user:delete:zhangsan")) { logger.info( "================== You are permitted to 'delete' the zhangsan with license plate (id) 'eagle5'." + "Here are the keys - have fun!"); } else { logger.info("================== Sorry, you aren't allowed to delete the 'eagle5' zhangsan!"); } // 执行登出,调用Subject的Logout方法 System.out.println("================== " + currentUser.isAuthenticated()); currentUser.logout(); System.out.println("================== " + currentUser.isAuthenticated()); System.exit(0); } }
如果需要完成整的代码请到以下网址进行下载: https://github.com/itzzuli/shiro-base.git
相关文章推荐
- 【shiro】入门程序
- shiro学习(一)---认证入门程序
- Shiro 学习记录 Shiro 入门程序
- shiro认证入门程序
- shiro授权入门程序
- shiro授权入门程序
- struts2 入门程序
- .NET Remoting程序开发入门篇(七)
- Spark入门(二):打包程序、提交任务到集群
- OPhone程序开发入门之音乐播放器
- C# 串口操作系列(2) -- 入门篇,为什么我的串口程序在关闭串口时候会死锁 ? .
- 入门第一周项目一—温度转换程序
- java入门程序100例学习笔记(006输出测试)
- Shiro基础功能之入门介绍
- VS2012入门图文教程——第一个程序Hello World
- [原]java专业程序代写(qq:928900200),学习笔记之基础入门<servlet_文件下载>(二十七)
- 一、python爬虫程序入门(图片下载)
- hadoop编程入门学习笔记-3 开发MapReduce程序
- 微信小程序入门视频
- shiro身份验证授权入门