开开始了解,有很多东西是不太明白的
1.进行登录验证:
Subject subject = SecurityUtils.getSubject();//先声明
UsernamePasswordToken shiroToken = new UsernamePasswordToken(json.getString("loginName"), MD5.getMd5(json.getString("passWord")));//提取用户名和密码 后台封装的是自动去搜索表,然后保存信息
// Boolean rememberMe = true; //是否记住
shiroToken.setRememberMe(true);
subject.login(shiroToken);//登录
BaseUser user = (BaseUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();//获取全局的信息
详见配置文件 spring-shiro.xml(注意看解释的部分):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"
>
<!-- 导入数据库的相关配置 -->
<import resource="classpath:spring-database.xml"/>
<!-- 对应于web.xml中配置的那个shiroFilter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- Shiro的核心安全接口,这个属性是必须的 -->
<property name="securityManager" ref="securityManager"/>
<!-- 要求登录时的链接(登录页面地址),非必须的属性,默认会自动寻找Web工程根目录下的"/login.jsp"页面,这个也是拦截器,如果没有登录的情况下,访问其中的方法,就回跳到这个方法中 -->
<property name="loginUrl" value="/login.html"/>
<!-- 登录成功后要跳转的连接(本例中此属性用不到,因为登录成功后的处理逻辑在LoginController里硬编码) -->
<!-- <property name="successUrl" value="/" ></property> -->
<!-- 用户访问未对其授权的资源时,所显示的连接 ,这个也是拦截器-->
<!--<property name="unauthorizedUrl" value="/error/unauthorized"/>-->
<!-- Shiro连接约束配置,即过滤链的定义 -->
<!-- 此处可配合这篇文章来理解各个过滤连的作用http://blog.csdn.net/jadyer/article/details/12172839 -->
<!-- 下面value值的第一个'/'代表的路径是相对于HttpServletRequest.getContextPath()的值来的 -->
<!-- anon:它对应的过滤器里面是空的,什么都没做,这里.do和.jsp后面的*表示参数,比方说login.jsp?main这种 ,表示不用登录认证就能访问的-->
<!-- authc:该过滤器下的页面必须验证后才能访问,它是Shiro内置的一个拦截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter 必须要登录认证之后才能访问的-->
<!--roles[内容] 表示需要内容所示的角色才能访问该路径-->
<!--perms[内容] 表示需要内容所示的权限才能访问该路径-->
<property name="filterChainDefinitions">
<value>
<!--静态资源可以匿名访问 -->
/js/** = anon
/css/** = anon
/img/** = anon
/common/** = anon
<!--登录验证码匿名访问-->
/login.html = anon
/login_manager.html = anon
/loginC/** = anon
/alLogin/** = anon
/authimage.gif = anon
<!--接口可以匿名访问-->
/alApi/** = anon
<!--用户头像-->
/photo/** = anon
<!--页面测试-->
/photoHtml/** = anon
<!--/doc.html = authc,roles[sysadmin] 接口文档地址-->
/doc.html = anon
<!--/swagger-ui.html = authc,roles[sysadmin]-->
<!--/** =authc-->
<!--任何链接都必须认证访问-->
/testC/** = ApiAuthc
/** = BaseAuthc
</value>
</property>
<!--<property name="filterChainDefinitions">-->
<!--<value>-->
<!--/admin/**=authc-->
<!--</value>-->
<!--</property>-->
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>
<!-- 数据库保存的密码是使用MD5算法加密的,所以这里需要配置一个密码匹配对象 -->
<!--<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.Md5CredentialsMatcher"></bean>-->
<!-- 缓存管理 -->
<!--<bean id="shiroCacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean>-->
<!-- 缓存管理器ehcache -->
<bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml"/>
</bean>
<!--
使用Shiro自带的JdbcRealm类
指定密码匹配所需要用到的加密对象
指定存储用户、角色、权限许可的数据源及相关查询语句
-->
<!--<bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
<!–<property name="credentialsMatcher" ref="credentialsMatcher"></property>–>
<property name="permissionsLookupEnabled" value="true"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="authenticationQuery"
value="SELECT password FROM base_user WHERE login_name = ?"></property>
<property name="userRolesQuery"
value="SELECT t.role_code from base_role t left join base_user tt on t.ROLE_CODE=tt.DEFAULT_ROLE_CODE WHERE LOGIN_NAME = ? "></property>
<property name="permissionsQuery"
value="SELECT PT.PERMISSION_NAME from PERMISSION_WITH pw LEFT JOIN PERMISSION_TEST pt on pw.P_ID = pt.PERMISSION_ID LEFT JOIN BASE_ROLE t on t.ROLE_ID = pw.ROLE_ID where t.role_code = ?"></property>
</bean>-->
<bean id="shiroRedisManager" class="org.crazycake.shiro.RedisManager">
<property name="host" value="${redis.hostName}:${redis.port}"/>
</bean>
<bean id="redisSessionDAO" class="org.crazycake.shiro.RedisSessionDAO">
<property name="redisManager" ref="shiroRedisManager" />
<property name="expire" value="3600" />
</bean>
<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="globalSessionTimeout" value="1800000"/>
<property name="sessionDAO" ref="redisSessionDAO" />
</bean>
<bean id="cacheManager" class="org.crazycake.shiro.RedisCacheManager">
<property name="redisManager" ref="shiroRedisManager" />
</bean>
<!-- Subject工厂 -->
<bean id="subjectFactory"
class="shiro.AgileSubjectFactory"/>
<!-- Shiro安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="shiroCacheManager"></property>
<property name="sessionManager" ref="sessionManager" />
<!--<property name="subjectFactory" ref="subjectFactory"/>
<property name="subjectDAO.sessionStorageEvaluator.sessionStorageEnabled"
value="false"/>-->
<!-- 配置多个Realm -->
<property name="authenticator" ref="authenticator"></property>
<property name="realms">
<list>
<ref bean="ShiroRealm"/>
<ref bean="AlRealm"/>
</list>
</property>
</bean>
<!-- 配置多个Realm -->
<bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
<property name="authenticationStrategy">
<bean class="org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy"></bean>
</property>
</bean>
<bean id="ShiroRealm" class="shiro.ShiroRealm"></bean>
<bean id="AlRealm" class="shiro.AlRealm"></bean>
<!-- Shiro的注解配置一定要放在spring-mvc中 -->
</beans>