一、依旧maven项目,追加Servlet坐标
<!-- servlet,jsp坐标 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
二、web.xml配置
<!-- shiro 的 web 环境的初始化:监听器 -->
<!--EnvironmentLoaderListener:在 web 应用中加载 shiro 的环境,加载 shiro.ini-->
<listener>
<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>
<!-- 默认加载 WEB-INF/shiro.ini 文件,如果需要修改路径 -->
<context-param>
<param-name>shiroConfigLocations</param-name>
<param-value>classpath:shiro.ini</param-value>
</context-param>
<!-- shiro 的过滤器 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
三、 定义 Realm
public class MyRealm extends AuthorizingRealm{
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollectionarg0) {
return null;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throwsAuthenticationException {
return null;
}
}
}
四、 配置 shiro.ini
[main]
myRealm=cn.hezhiyu.realms.MyRealm
securityManager.realm=$myRealm
PS:shiro的web内置过滤器(重点)
shiro的内置过滤器,shiro自己提供一些专门的认证,授权过滤器。
shiro分为两种过滤器
1、认证过滤器:
anon:用户不需要认证也可以访问
authc:用户必须认证才可以访问
user:用户只要remeberMe,就可以访问
2、授权过滤器
perms:基于资源的授权过滤器
roles:基于角色的授权过滤器
例如:
[main]
myRealm=cn.hezhiyu.realms.MyRealm
securityManager.realm=$myRealm
[urls] user:用户只要remeberMe,就可以访问
/index.jsp=authc
五、编写shrio的认证操作
1、登录页面
<h3>用户登录</h3>
<font color="red">${msg}</font>
<form method="post" action="${pageContext.request.contextPath}/login">
用户名:<input type="text" name="name"/><br/>
密码:<input type="password" name="password"/><br/>
<input type="submit" value="登录">
</form>
六、编写servlet
publicclass LoginServlet extendsHttpServlet {
privatestaticfinallongserialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponseresponse) throwsServletException, IOException {
//1.设置请求编码
request.setCharacterEncoding("utf-8");
//2.接收用户名和密码
String name = request.getParameter("name");
String password = request.getParameter("password");
//3.调用login方法
//3.1 获取Subject
Subject subject =SecurityUtils.getSubject();
AuthenticationToken token = newUsernamePasswordToken(name, password);
try {
subject.login(token);
//获取Principal
String dbName =(String)subject.getPrincipal();
//把用户信息存储到session
request.getSession().setAttribute("userName", name);
//登录成功
response.sendRedirect(request.getContextPath()+"/index.jsp");
} catch(UnknownAccountException e) {
request.setAttribute("msg", "用户名不存在");
request.getRequestDispatcher("/login.jsp").forward(request, response);
} catch(IncorrectCredentialsException e) {
request.setAttribute("msg", "密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
protectedvoiddoPost(HttpServletRequest request, HttpServletResponseresponse) throwsServletException, IOException {
doGet(request, response);
}
七、编写 Realm的认证方法
@Override
protected AuthenticationInfodoGetAuthenticationInfo(AuthenticationToken arg0) throwsAuthenticationException {
System.out.println("执行了认证方法");
//1.获取用户输入的账户信息
UsernamePasswordToken token =(UsernamePasswordToken)arg0;
//模拟数据库的密码
String name = "jack";
String password = "1234";
if(!token.getUsername().equals(name)){
//用户不存在
returnnull;
}
//返回密码
returnnew SimpleAuthenticationInfo(name,password,"");
}
注意:login 登录请求必须使用 anon 放行
[urls]
/login=anon
八、编写shrio的授权操作
[urls]
/product/add.jsp=perms[product:add]
配置未授权的页面:
[main]
perms.unauthorizedUrl=/unauth.jsp
九、编写 Realm的授权逻辑
@Override
protectedAuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
System.out.println("执行了授权方法");
SimpleAuthorizationInfo info = newSimpleAuthorizationInfo();
//添加资源授权码
info.addStringPermission("product:add");
//添加角色授权码
info.addRole("admin");
return info;
}
PS:
shiro.inl:
[main]
perms.unauthorizedUrl=/unauth.jsp
roles.unauthorizedUrl=/unauth.jsp
myRealm=cn.hezhiyu.MyRealm
securityManager.realm=$myRealm
[urls]
/product/list.jsp=anon
/login=anon
/product/add.jsp=perms[product:add]
/product/update.jsp=roles[admin]
/**=authc