本菜鸡刚开始学习shiro框架百思不得其解。其因是代码理解能力不强和基础功不是很扎实。各种网上搜索看帖、看源码(下载代码),最后结论是接不上头。着实无奈!!!
偶得一套学习秘籍抱试一试心态,还是有不小收获。以下笔记纯属记录小菜鸡学习历程。大神勿喷。
-------:SpringMvc +Mybtais
Shiro 开头篇:引入Shiro所需要的Jar包
<!--shiro --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.3.2</version> </dependency> <!-- shiro ehcache --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <exclusions> <exclusion> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> </exclusion> </exclusions> <version>1.4.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-cache --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>2.1.3.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.4</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> <exclusions> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.theborakompanioni</groupId> <artifactId>thymeleaf-extras-shiro</artifactId> <version>2.0.0</version> </dependency> <!-- utils --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.31</version> </dependency> <!--velocity代码生成使用模板 --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> <exclusions> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency>
这是迈入深坑的一大步,也是第一步。
接着我们把web.xml中的 contextConfigLocation 和 Servlet 配置好,加上Shiro.ini文件 后面会用到。笔记记下来要考。
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:appliationContent.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-serlvet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
紧跟着上文配置中的AppliationContent.xml 引入Shiro 快速实例的配置:shiro-shiro-root-1.3.2版本,其代码各路英雄好汉处仓库均有销售。
打开Web-Info中 找到重要的两个文件:
获取web.xml中的代码 重要的是别忘了加入Shiro的拦截器到自己的的Web.xml中。另一个代码完全Copy到自己项目中的AppliationContent.xml就好。
<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>
好了,虽然测试的第一步代码和以上需要的配置关联的不多但终归是要用到的,饭不能一次一粒一粒的吃。废话不多说上菜。
测试代码:-------------------------------------------------------------------------------------------------
log.info("My First Apache Shiro Application"); Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); // get the currently executing user: Subject currentUser = SecurityUtils.getSubject(); Session session = currentUser.getSession(); session.setAttribute( "someKey", "aValue" ); String value = (String) session.getAttribute("someKey"); if (value.equals("aValue")) { log.info("<<<<<<< Retrieved the correct value! [" + value + "]"); } //获取当前交互的用户信息 if ( !currentUser.isAuthenticated() ) { //collect user principals and credentials in a gui specific manner //such as username/password html form, X509 certificate, OpenID, etc. //We'll use the username/password example here since it is the most common. UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa"); //this is all you have to do to support 'remember me' (no config - built in!): token.setRememberMe(true); try { currentUser.login(token); //if no exception, that's it, we're done! } catch (UnknownAccountException uae) { //username wasn't in the system, show them an error message? } catch (IncorrectCredentialsException ice) { //password didn't match, try again? } catch (LockedAccountException lae) { //account for that username is locked - can't login. Show them a message? }catch ( AuthenticationException ae ) { //unexpected condition - error? } } log.info(">>>>>> User [" + currentUser.getPrincipal() + "] logged in successfully."); //test a role: if (currentUser.hasRole("schwartz")) { log.info("-------May the Schwartz be with you!"); } else { log.info("Hello, mere mortal."); } //test a typed permission (not instance-level) if (currentUser.isPermitted("lightsaber:weild")) { log.info("---------You may use a lightsaber ring. Use it wisely."); } else { log.info("Sorry, lightsaber rings are for schwartz masters only."); } //a (very powerful) Instance Level permission: if (currentUser.isPermitted("winnebago:drive:eagle5")) { log.info("--------You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " + "Here are the keys - have fun!"); } else { log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!"); } currentUser.logout(); //removes all identifying information and invalidates their session too.
相信很多看过官网说明的小菜鸡都见过以上代码,我就不多解释了。