shiro的使用1 简单的认证
Posted isme-zjh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shiro的使用1 简单的认证相关的知识,希望对你有一定的参考价值。
有空学了一个简单的安全框架shiro,资料比较少,在百度和google上能搜到的中文我看过了,剩下的时间有空会研究下官网的文章和查看下源码,
简单的分享一些学习过程;
1,简单的一些概念上的认知
2,使用认证的基本流程
3,shiro集成spring完成简单的认证流程,已实现
1 | 建一个maven的web项目,引入依赖 springmvc的的依赖 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.0.RELEASE</version> </dependency> shiro跟spring集成的插件 <dependency>
<groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.2.3</version> </dependency> |
|
2 | 配置web.xml 指出spring容器的配置文件位置 <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:context_config.xml</param-value> </context-param> 指出spring在web容器中的代号 <context-param> <param-name>webAppRootKey</param-name> <param-value>wechatSystem</param-value> </context-param> 初始化spring的容器 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> spring mvc的分发器 <servlet> <servlet-name>admin</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:admin-dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>admin</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> spring跟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>/admin/*</url-pattern> </filter-mapping> <!-- 字符过滤,保存中文的时候用到 --> <filter> <filter-name>characterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
|
3 | 配置shiroFilter实例 <!--shiro的配置,关键两点,配置SecurityManager和依赖的RealM--> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /> <property name="loginUrl" value="/admin/login" /> <property name="successUrl" value="/admin/home" /> <property name="unauthorizedUrl" value="/admin/login" /> <property name="filters"> <map> <entry key="anno" value-ref="anno"/> <entry key="authc" value-ref="authc"/> </map> </property> <property name="filterChainDefinitionMap"> <map> <entry key="anon" value="anon"/> <entry key="authc" value="authc"/> </map> </property> <property name="filterChainDefinitions"> <value> /admin/login=anon /admin/validCode=anon /user/**=authc /role/**=authc /permission/**=authc /**=authc </value> </property> </bean> <bean id="authc" class="com.util.filter.MyAccessFilter"/> <bean id="anno" class="org.apache.shiro.web.filter.authc.AnonymousFilter"/> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="myRealm"/> </bean> <bean id="myRealm" class="com.util.MysqlJdbcRealM"/> |
|
4 | 开发跟shiro交互的RealM,一般把权限信息放到db中
|
|
5 | 简单的登录页面,功能测试 |
|
6 | controller的实现:
|
|
7 | 指定认证的策略和多数据源 <!--shiro的配置--> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /> <property name="loginUrl" value="/admin/login" /> <property name="successUrl" value="/admin/home" /> <property name="unauthorizedUrl" value="/admin/login" /> <property name="filters"> <map> <entry key="anno" value-ref="anno"/> <entry key="authc" value-ref="authc"/> </map> </property> <property name="filterChainDefinitionMap"> <map> <entry key="anon" value="anon"/> <entry key="authc" value="authc"/> </map> </property> <property name="filterChainDefinitions"> <value> /admin/login=anon /admin/validCode=anon /user/**=authc /role/**=authc /permission/**=authc /**=authc </value> </property> </bean> <bean id="authc" class="com.util.filter.MyAccessFilter"/> <bean id="anno" class="org.apache.shiro.web.filter.authc.AnonymousFilter"/> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="authenticator" ref="modelAuthricator"/> </bean> <bean id="modelAuthricator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator"> <property name="authenticationStrategy" ref="firstSuccess"/> <property name="realms"> <list> <ref local="myRealm"/> </list> </property> </bean> <bean id="firstSuccess" class="org.apache.shiro.authc.pam.FirstSuccessfulStrategy"/> |
以上是关于shiro的使用1 简单的认证的主要内容,如果未能解决你的问题,请参考以下文章
ShiroApache Shiro架构之权限认证(Authorization)