引入shiro-all-1.2.3.jar
web.xml中加入,部分
<!-- Shiro filter-->
<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>
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<url-pattern>*.shtml</url-pattern>
</filter-mapping>
在application.xml中加入
<!-- Shiro开始 -->
<bean id="securityManager"
class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="shiroDbRealm" />
</bean>
<!-- 項目自定义的Realm -->
<bean id="shiroDbRealm" class="com.my.shiro.ShiroDbRealm" ></bean>
<!-- Shiro Filter -->
<bean id="shiroFilter"
class="org.apache.shiro.spring.web.ShiroFilterFactoryBean" >
<!-- 必须的securityManager -->
<property name="securityManager" ref="securityManager" />
<!-- 没有登录的用户请求需要登录的页面时自动跳转到登录页面,不是必须的属性,不输入地址的话会自动寻找项目web项目的根目录下的”/login.jsp”页面 -->
<property name="loginUrl" value="/party/login.shtml" />
<!-- 登录成功默认跳转页面,不配置则跳转至”/”。如果登陆前点击的一个需要登录的页面,则在登录自动跳转到那个需要登录的页面。不跳转到此。 -->
<property name="successUrl" value="/" />
<!-- 没有权限默认跳转的页面 -->
<property name="unauthorizedUrl" value="/party/unauthorized.shtml" />
<!-- -就是需要验证的地址的列表 认证过滤器:anon,authcBasic,auchc,
授权过滤器:user perms,roles, ssl,rest,port
anon不过滤,authc登录认证过滤,roles,需要相应的角色,授权过滤
Shiro验证规则,是从上下向下过滤的,只要第一个满足,下面就不再过滤。
如 /user/login** = anon,不过滤,
/** = authc,表示所有链接都要过滤,但/user/login**已经在第一个,不过滤,
/** = authc这个过滤,就对他无效。
-->
<property name="filterChainDefinitions">
<value>
/user/login** = anon
/user** = authc
/role/**=roles["admin"]
/** = authc
</value>
<property/>
</bean>
<!-- 开启Shiro注解的Spring配置方式的beans。在lifecycleBeanPostProcessor之后运行 -->
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor" />
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>
<bean id="lifecycleBeanPostProcessor"
class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.apache.shiro.authz.UnauthorizedException">/user/login.shtml</prop>
<prop key="org.apache.shiro.authz.UnauthenticatedException">/user/unauthorized.shtml</prop>
</props>
</property>
</bean>
<!-- Shiro结束 -->
自定义的ShiroDbRealm ,用来实现认证和授权的功能。
注意认证,即登录操作时要在登录操作中,把用户,密码存入Shrio的token对象,这样才能在ShiroDbRealm 使用
/**
* 登录操作
* 把用户信息存入Token中
* @return
*/
public String loginSubmit() {
Subject user = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(userName,password);
token.setRememberMe(true);
user.login(token);
return "success";
}
自定义的ShiroDbRealm ,
package com.my.shiro.;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import com.shrio.model.User;
import com.asiainfo.mnt.console.party.service.impl.PartyService;
public class ShiroDbRealm extends AuthorizingRealm{
@Resource
private UserService userService;
/**
* 授权信息
* @1把用户的角色存入认证对象中
* @2再通过spring配置文件,对应要授权的访问路径资源,进行角色配置授权
* 例如/admins/user/**=roles["admin"]
* 拥有admin角色的用户,才能访问/admins/user/路径下的资源
* 或直接使用@RequiresRoles
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String username=(String)principals.fromRealm(getName()).iterator().next();
if( username != null ){
User user= userService.selectByUserName(username);
if( user!= null && user.getRoles() != null ){
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
for (int i = 0; i < user.getRoles().size(); i++) {
//把角色信息存储到授权信息info中
info.addRole(user.getRoles().get(i).getRoleName());
}
//把权限信息存储到授权信息info中
// info.addStringPermissions(each.getPermissionsAsString());
return info;
}
}
return null;
}
/**
* 认证信息
* 认证当前用户,是否登录,是否符合认证规则
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authcToken ) throws AuthenticationException {
//获取用户令牌中存储的用户信息
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
String userName = token.getUsername();
if( userName != null && !"".equals(userName) ){
//通过用户名和密码,获取对应的user对象,再将获得的user,放到认证对象中
Map<String, String> map = new HashMap<String, String>();
map.put("partyName", token.getUsername());
map.put("partyPassword", String.valueOf(token.getPassword()));
User user= userService.getUserByNameAndPassword(map);
if( user!= null )
return new SimpleAuthenticationInfo(
user.getUserName(),user.getUserPassword(), getName());
}
return null;
}
}