判断你(被认证者)是谁的过程。通常被认证者提供用户名和密码。
常见的认证包含如下几种:
判断被认证者(你)是否能做什么操作的过程。
perms org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
port org.apache.shiro.web.filter.authz.PortFilter
roles org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
ssl org.apache.shiro.web.filter.authz.SslFilter
使用技术手段(如:MD5、SHA等)把待加密的数据变为密文(如:信息摘要等)过程。
基于角色的访问控制(Role-Based Access Control)。
data access object for an application’s security components (users,roles, permissions)
最小粒度的授权,不与用户关联。
例如:导出报表、查看id号为“PO20090008”的采购单、创建FAQ。
Permission的集合。
- 简单。
- 功能强大。
- 能独立运行,不依赖其它框架或容器。
- 包含了认证、授权、Session管理、加密。
- 易于扩展。
用户账号Account,可以简单的理解为用户。
一个账号可以拥有多个角色(Role)。
一个角色包含了多个权限(Permission)。
Eclipse:File--New--Other--Web--Dynamic Web Project
在 /WEB-INFO/lib/目录下添加如下Jar包
相关Jar包,http://incubator.apache.org/shiro/download.html
ShiroFilter
org.apache.shiro.web.servlet.IniShiroFilter
ShiroFilter
/*
[main]
#SHA256加密
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
#realm
myRealm = com.xx.xx.shiro.MyShiroRealm
myRealm.credentialsMatcher = $sha256Matcher
#缓存
myRealm.authorizationCachingEnabled = true
cache=org.apache.shiro.cache.ehcache.EhCacheManager
myRealm.cacheManager=$cache
[filters]
shiro.loginUrl = /login.jsp
#authc=org.apache.shiro.web.filter.authc.FormAuthenticationFilter
authc.successUrl =/background.jsp
perms.unauthorizedUrl =/401.jsp
[urls]
/login.jsp=authc
/logout.jsp=anon
/about.jsp=anon
/background.jsp=authc
/faq/test.jsp=authc
/faq/list.jsp=authc,perms["faq:list"]
/faq/view.jsp=authc,perms["faq:view"]
位置:
配置参数可以写在web.xml文件中,也可以单独文件形式存放在本地类根路径、文件系统以及网络环境中。
Shiro INI Inline Config 和External Config
public class MyShiroRealm extends AuthorizingRealm {
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
String username = (String) principals.fromRealm(
getName()).iterator().next();
if( username != null ){
AccountManager accountManager = new AccountManagerImpl();
Collection myRoles = accountManager.getRoles( username );
if( myRoles != null ){
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
for( Role each:myRoles ){
info.addRole(each.getName());
info.addStringPermissions( each.getPermissionsAsString() );
}
return info;
}
}
return null;
}
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authcToken ) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
String accountName = token.getUsername();
//用户名密码验证
if( accountName != null && !"".equals(accountName) ){
AccountManager accountManager = new AccountManagerImpl();
Account account = accountManager.get( token.getUsername() );
if( account != null )
return new SimpleAuthenticationInfo(
account.getName(),account.getPassword(), getName() );
}
return null;
}
}
<%Object obj = request.getAttribute(org.apache.shiro.web.filter.authc.
FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
boolean flag = false;
String msg = "";
if( obj != null ){
if( "org.apache.shiro.authc.UnknownAccountException".equals( obj ) )
msg = "未知帐号错误!";
else if("org.apache.shiro.authc.IncorrectCredentialsException".equals( obj ))
msg = "密码错误!";
else if( "org.apache.shiro.authc.AuthenticationException".equals( obj ))
msg = "认证失败!";
flag = !"".equals(msg);
}
if( flag )
out.print( msg );
%>
<%SecurityUtils.getSubject().logout();%>
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;
public class CaptchaFormAuthenticationFilter extends FormAuthenticationFilter{
public static final String DEFAULT_CAPTCHA_PARAM = "captcha";
private String captchaParam = DEFAULT_CAPTCHA_PARAM;
public String getCaptchaParam() {
return captchaParam;
}
protected String getCaptcha(ServletRequest request) {
return WebUtils.getCleanParam(request, getCaptchaParam());
}
protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) {
String username = getUsername(request);
String password = getPassword(request);
String captcha = getCaptcha(request);
boolean rememberMe = isRememberMe(request);
String host = getHost(request);
return new CaptchaUsernamePasswordToken(username, password, rememberMe, host,captcha);
}
}
import org.apache.shiro.authc.UsernamePasswordToken;
public classCaptchaUsernamePasswordToken extends UsernamePasswordToken {
private static final long serialVersionUID = 1L;
private String captcha;
public String getCaptcha() {
return captcha;
}
public void setCaptcha(String captcha) {
this.captcha = captcha;
}
public CaptchaUsernamePasswordToken() {
super();
}
public CaptchaUsernamePasswordToken(String username, char[] password,
boolean rememberMe, String host,String captcha) {
super(username, password, rememberMe, host);
this.captcha = captcha;
}
}
public classIncorrectCaptchaException extends AuthenticationException{
private static final long serialVersionUID = 1L;
public IncorrectCaptchaException() {
super();
}
public IncorrectCaptchaException(String message, Throwable cause) {
super(message, cause);
}
public IncorrectCaptchaException(String message) {
super(message);
}
public IncorrectCaptchaException(Throwable cause) {
super(cause);
}
}
authc= com.xx.xx.shiro.CaptchaFormAuthenticationFilter
protectedAuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authcToken ) throwsAuthenticationException {
CaptchaUsernamePasswordToken token = (CaptchaUsernamePasswordToken) authcToken;
String accountName = token.getUsername();
//验证码 验证
String captcha = null;
Object obj_captcha = SecurityUtils.getSubject().getSession().getAttribute( SessionKey.CAPTCHA );
Object obj_count = SecurityUtils.getSubject().getSession().getAttribute( SessionKey.LOGIN_FAILED_COUNT );
int failed_count = (obj_count == null || !(obj_count instanceof Integer))?0:(Integer)obj_count;
if( obj_captcha instanceof String)
captcha = (String)obj_captcha;
if( captcha != null && failed_count >0&& !captcha.equalsIgnoreCase( token.getCaptcha() )){
throw newIncorrectCaptchaException("验证码错误!");
}
//用户名密码验证
if( accountName != null && !"".equals(accountName) ){
AccountManager accountManager = newAccountManagerImpl();
Account account = accountManager.get( token.getUsername() );
if( account != null )
return new SimpleAuthenticationInfo( account.getName(),account.getPassword(), getName() );
}
return null;
}
}
<% Object obj = request.getAttribute(org.apache.shiro.web.filter.authc.
FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
boolean flag = false;
String msg = "";
if( obj != null ){
if( "org.apache.shiro.authc.UnknownAccountException".equals( obj ) )
msg = "未知帐号错误!";
else if("org.apache.shiro.authc.IncorrectCredentialsException".equals( obj ))
msg = "密码错误!";
else if("com.xx.xx.shiro.IncorrectCaptchaException".equals( obj ))
msg = "验证码错误!";
else if( "org.apache.shiro.authc.AuthenticationException".equals( obj ))
msg = "认证失败!";
flag = !"".equals(msg);
}
if( flag ){
out.print( msg );
Integer count = (Integer)request.getSession().getAttribute(SessionKey.LOGIN_FAILED_COUNT );
if( count == null )
count = Integer.valueOf(0);
count++;
request.getSession().setAttribute(SessionKey.LOGIN_FAILED_COUNT, count);
}
%>
JAVA1.6
Tomcat
Eclipse