首先在web.xml中配置过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>shiro</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 监听器启动spring框架 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext/applicationContext-*.xml</param-value>
</context-param>
<!-- 字符过滤器 -->
<filter>
<filter-name>encodingFilter</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>
</filter>
<!-- shiro过虑器,DelegatingFilterProxy通过代理模式将spring容器中的bean和filter关联起来 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<!-- 设置true由servlet容器控制filter的生命周期 -->
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
<!-- 设置spring容器filter的bean id,如果不设置则找与filter-name一致的bean -->
<init-param>
<param-name>targetBeanName</param-name>
<param-value>shiroFilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<!-- 拦截根路径下的所有请求 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<!-- 对springmvc处理的请求进行转码 -->
<servlet-name>springmvc</servlet-name>
</filter-mapping>
<!-- springmvc配置 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
编写applicationContext-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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- web.xml中shiro的filter对应的bean -->
<!-- Shiro 的Web过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 设置安全管理器 -->
<property name="securityManager" ref="securityManager" />
<!-- loginUrl认证提交地址,如果没有认证将会请求此地址进行认证,
请求此地址将由formAuthenticationFilter进行表单认证 -->
<property name="loginUrl" value="/login.do" />
<!-- 认证成功统一跳转到index.do,建议不配置,shiro认证成功自动到上一个请求路径 -->
<property name="successUrl" value="/index.do" />
<!-- 通过unauthorizedUrl指定没有权限操作时跳转页面 -->
<property name="unauthorizedUrl" value="/refuse.do" />
<!-- 过虑器链定义,从上向下顺序执行,一般将/**放在最下边 -->
<property name="filterChainDefinitions">
<value>
<!-- /** = authc 所有url都必须认证通过才可以访问 /refuse.do=anon -->
/login.do=authc
/logout=logout
<!-- /user/list.do=perms[role:list] -->
/** = anon
<!-- /** = anon所有url都可以匿名访问 -->
</value>
</property>
</bean>
<!-- 安全管理器定义 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="customRealm"></property>
<property name="sessionManager" ref="sessionManager"></property>
</bean>
<!-- 自定义Realm -->
<bean id="customRealm" class="com.llxy.shiro.realm.CustomRealm">
<!-- 定义凭证匹配器 -->
<property name="credentialsMatcher" ref="credentialsMatcher"></property>
</bean>
<!-- 凭证匹配器 -->
<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<!-- 加密算法名称 -->
<property name="hashAlgorithmName" value="md5"></property>
<!-- 散列次数 -->
<property name="hashIterations" value="2"></property>
</bean>
<!-- 会话管理器 -->
<bean id="sessionManager" class="org.apache.shiro.session.mgt.DefaultSessionManager">
</bean>
</beans>
自定义的Realm类
package com.llxy.shiro.realm;
import java.util.HashSet;
import java.util.Set;
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.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import com.llxy.shiro.service.PermissionService;
import com.llxy.shiro.service.UserService;
import com.llxy.shiro.vo.Permission;
import com.llxy.shiro.vo.User;
public class CustomRealm extends AuthorizingRealm {
@Autowired
private UserService userService;
@Autowired
private PermissionService permissionService;
@Override
public String getName() {
return "CustomRealm";
}
/**
* 完成认证
* */
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException {
// 完成认证信息验证
String userName = token.getPrincipal().toString();
User currentUser = null;
try {
currentUser = userService.login(userName);
// 设置菜单
currentUser.setMenus(permissionService.findMenuByUser(currentUser.getId()));
// 设置权限
currentUser.setPermissions(permissionService.findPermissionByUser(currentUser.getId()));
} catch (Exception e) {
e.printStackTrace();
}
if (currentUser == null) {
return null;
}
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(
currentUser, currentUser.getPwd(),
ByteSource.Util.bytes(currentUser.getSalt()), getName());
return info;
}
/**
* 完成授权
* */
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principalCollection) {
// 获取用户名
User currentUser = (User) principalCollection.getPrimaryPrincipal();
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
Set<String> permission = new HashSet<String>();
for (Permission p : currentUser.getPermissions()) {
permission.add(p.getPercode());
}
info.addStringPermissions(permission);
return info;
}
}
测试用的controller
@Controller
public class SystemController {
// 用户登陆提交
@RequestMapping("/login")
public String loginsubmit(Model model, HttpServletRequest request)
throws Exception {
// shiro在认证过程中出现错误后将异常类路径通过request返回
String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
if (exceptionClassName != null) {
if (UnknownAccountException.class.getName().equals(
exceptionClassName)) {
throw new Exception("账号不存在");
} else if (IncorrectCredentialsException.class.getName().equals(
exceptionClassName)) {
throw new Exception("用户名/密码错误");
} else if ("randomCodeError".equals(exceptionClassName)) {
throw new Exception("验证码错误");
} else {
throw new Exception();// 最终在异常处理器生成未知错误
}
} else {
return "home";
}
}
// 系统首页
@RequestMapping("/index")
public ModelAndView index(ModelMap map) {
Subject subject = SecurityUtils.getSubject();
User currentUser = (User) subject.getPrincipal();
map.addAttribute("currentUser", currentUser);
return new ModelAndView("index");
}
}
登录页面
<html>
<body>
<form action="login.do" method="post">
<input type="text" name="name" />
<br>
<input type="password" name="pwd" />
<br>
<input type="submit" value="登录" />
</form>
</body>
</html>