Shiro 入门
来源:互联网 发布:c语言强制转换 编辑:程序博客网 时间:2024/05/23 14:28
Shiro可以在任意Java环境下运行,比如Application,Web等等
Application最简单,导入Jar包就可以写程序了
/* * 默认端口27017,应该可以修改的 */public class Test {public static void main(String[] args) throws UnknownHostException, MongoException {Mongo mg = new Mongo();DB db = mg.getDB("imooc");for(String name : db.getCollectionNames())System.out.println(name);DBCollection coll = db.getCollection("imooc_collection");DBObject add = new BasicDBObject();add.put("name", "yiwei");System.out.println(coll.save(add).getN());//getAll(coll);//coll.remove(new BasicDBObject("_id", new ObjectId("594de3e223edb65e547dd6b5")));//coll.remove(new BasicDBObject("name", "yiwei"));//getAll(coll);System.out.println(coll.find(new BasicDBObject("name", "yiwei")).toArray());}public static void getAll(DBCollection coll) {DBCursor cur = coll.find();while(cur.hasNext()) {System.out.println(cur.next());}}}
Web可以用maven,也可以不用,不用就是导入Jar包;可以用Spring来管理Bean;可以用SpringMVC来处理URI请求
这里以整合Spring为例,实现登陆拦截:
现在Web里面配置启动Shiro
<filter> <filter-name>shiroFilter</filter-name> <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
这样Shiro就能拦截所有的请求
在applicationContext里面配置org.apache.shiro.spring.web.ShiroFilterFactoryBean的拦截链信息
<!-- Shiro Filter配置 --><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"><property name="securityManager" ref="securityManager"></property><property name="loginUrl" value="/login.jsp"></property><property name="successUrl" value="/index.jsp"></property><property name="unauthorizedUrl" value="/login.jsp"></property><property name="filterChainDefinitions"><value>/login.jsp = authc /login.do* = anon /pages/* = authc /index.jsp* = authc</value></property></bean><!-- securityManager --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="myRealm" /> </bean> <!-- 自定义Realm实现 --> <bean id="myRealm" class="realm.CustomRealm" />
这样Shiro拦截到请求,发现user要访问某些页面,然后自己查了一下自己的水表,发现user没有登录,而这个页面又是需要登录的,就跳转到loginURL,
当用户填好表格,提交到Servlet后,在Servlet中尝试进行login,并自动(securityManager中含有自定义的Realm对象)在Realm中与DAO进行交互,判断是否成功,然后跳转页面
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {Subject currentUser = SecurityUtils.getSubject();UsernamePasswordToken token = new UsernamePasswordToken( request.getParameter("username"), request.getParameter("password")); token.setRememberMe(true); try { /* * 在调用了login方法后,SecurityManager会收到AuthenticationToken,并将其发送给已配置的Realm * ,执行必须的认证检查。每个Realm都能在必要时对提交的AuthenticationTokens作出反应。 * 但是如果登录失败了会发生什么?如果用户提供了错误密码又会发生什么?通过对Shiro的运行时AuthenticationException做出反应 * ,你可以控制失败 */ currentUser.login(token); request.getRequestDispatcher("/index.jsp").forward(request, response); } catch (AuthenticationException e) {//登录失败 e.printStackTrace(); request.setAttribute("msg", "不匹配的用户名和密码"); request.getRequestDispatcher("/login.jsp").forward(request, response); } }
public class CustomRealm extends AuthorizingRealm{/* * 回调函数,提取当事人的角色和权限 * principals 当事人 */protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {String username = (String) principals.fromRealm(getName()).iterator().next();/*这些代码应该是动态从数据库中取出的,此处写死*/ if(username!=null && username.equals("yi")){ SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); info.addRole("admin");//添加一个角色,不是配置意义上的添加,而是证明该用户拥有admin角色 info.addStringPermission("admin:manage");//添加权限 return info; } return null; }protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {//令牌——基于用户名和密码的令牌 // UsernamePasswordToken token = (UsernamePasswordToken) authcToken; //令牌中可以取出用户名密码 // String accountName = token.getUsername(); /*此处无需比对,比对的逻辑Shiro会做,我们只需返回一个和令牌相关的正确的验证信息, * 因此在随后的登录页面上只有admin/admin123才能通过验证*/ return new SimpleAuthenticationInfo("yi", "wei", getName());}}
boot 没有xml文件,如果想整合Springboot的话需要Java Config,同时注意加上Scan注解
阅读全文
0 0
- shiro入门
- shiro入门
- Shiro入门
- shiro入门
- shiro入门
- Shiro 入门
- Shiro 入门
- Shiro 入门
- Shiro 入门
- shiro入门
- shiro入门
- shiro入门
- shiro入门
- shiro入门
- Shiro入门(1)
- shiro入门(1)
- shiro入门实例
- shiro入门实例
- Linux中硬链接和软链接的区别
- Linux C++ 连接MySQL
- Python知识点
- 【怎样写代码】向现有类型“添加”方法 -- 扩展方法(一):扩展方法概述
- linux 文件编程
- Shiro 入门
- leetcode628. Maximum Product of Three Numbers
- Java中的类型转换
- 美团 CodeM 资格赛 Round A 合并回文子串
- 概率论的一些基本问题
- Linux学习基础篇(九)
- 序列选择器
- Mybatis自动映射
- gpu并行编程简略笔记