标签:shiro filter spring security
由于项目的需要,最近开始研究shiro这个框架。shiro是一个安全框架,主要是验证和授权管理,和它类似的有spring security框架,当然,spring security框架更加强大,但是shiro更加灵活(一般小的东西都比较灵活)。
对于shiro,网上的资料可谓是少之又少。基本上算是被开涛大魔王的一个《跟我学shiro》垄断,当然这个教程是很全面,楼主也是跟着这个教程一点点学的(没办法,没有别的资料啊,很多看不懂的地方没地方找啊,shiro官网的英文看不懂啊卧槽)。虽然过程很痛苦,还好最近对这个框架基本上了解了一些,所以现在敢记录记录。
这里就不从头开始介绍什么ini配置了,一般都是集成在spring项目里面的,使用maven进行项目管理,这一章先简单的写点基础配置吧。代码是学习的时候写的,还有待修改的地方,不过学习足够了。
<!-- 配置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> @Component
public class UserRealm extends AuthorizingRealm {
@Autowired
private IUserService userService;
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
String username= (String) principalCollection.getPrimaryPrincipal();
User user=userService.findByUsername(username);
//System.out.println("授权"+user);
if (user!=null)
{
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
// System.out.println(userService.findRole(username));
info.addRole(userService.findRole(username));
info.addStringPermission(userService.findPermissions(username));
// System.out.println(info);
return info;
}
else throw new IncorrectCredentialsException();
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
String username= (String) authenticationToken.getPrincipal();
String password=new String((char[])authenticationToken.getCredentials());
User user=userService.findByUsername(username);
System.out.println(user);
if(user!=null)
{
if(password.equals(user.getPassword())){
Session session= SecurityUtils.getSubject().getSession();
session.setAttribute("username",user.getUsername());
return new SimpleAuthenticationInfo(username,password,getName());
}
else throw new UnknownAccountException();
}
else
{
throw new UnknownAccountException();
}
} <!--配置shiroFilter-->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!-- 登录路径-->
<property name="loginUrl" value="/jsp/login.jsp"/>
<!--登录成功路径-->
<property name="successUrl" value="/jsp/loginSuccess.jsp"/>
<!--授权失败路径-->
<property name="unauthorizedUrl" value="/jsp/unauthorized.jsp"/>
<property name="filters">
<util:map>
<entry key="authc" value-ref="formAuthenticationFilter"/>
<entry key="stateless" value-ref="statelessFilter"/>
<!--<entry key="ssl" value-ref="sslFilter"/>-->
</util:map>
</property>
<!--过滤链定义-->
<property name="filterChainDefinitions">
<value>
/jsp/login.jsp=anon
/jsp/loginSuccess.jsp=authc
/jsp/logout.jsp=logout
/jsp/success.jsp=user
/static/**=anon
/hello**=stateless
</value>
</property>
</bean> 2)它还得需要一个叫安全管理器的东东:
<!--配置securityManager-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="userRealm"/>
</bean> 3)安全管理器需要一个Realm的东东,就是刚才自己写的那个,配置上就可以了: <!--Realm实现-->
<bean id="userRealm" class="com.pps.sps.realm.UserRealm" >
</bean> 4)shiro的生命周期管理器,人家都加了,我不加也不好看: <!-- Shiro生命周期处理器-->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
倘若需要使用注解,在springMVC的配置文件中引入这个配置文件,加上注解的配置就可以了:
<!--使用shiro注解-->
<import resource="spring-shiro.xml"/>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true"></property>
</bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"></property>
</bean>标签:shiro filter spring security
原文地址:http://blog.csdn.net/js_sky/article/details/45565493