最近看了一下shiro源码,觉得shiro做有关权限系统挺方便的,很多的东西它都是默认了的,根据实际情况重写一些方法,便可应用到实际开发中。扩展性非常强,灵活性高,是一个不错的优秀开源框架,通过阅读源码,运用到一些设计模式,还是挺值得去学习的。
shiro框架图
Subject 主体:可以是当前的用户,可以是第三方服务。
Security Manager 安全管理器:负责协调各个组件的。
Authenticator:登录认证器,登录时用到。
Authorizer:权限认证器,当访问的资源需要权限校验时会调用。
realm:使用shiro框架登录与鉴权时,一定要至少配置一个realm,实现其中的那个两个方法,一个是身份信息(AuthenticationInfo)封装返回,一个是权限信息(AuthorizationInfo)封装返回。
shiro登录校验流程及权限校验流程
登记认证时可以通过FormAuthenticationFilter进行验证,也可以直接通过subje.login(token),其实FormAuthenticationFilter在做检验时,间接调用了subje.login(token)方法进行权限校验。
shiro cookie信息创建
//shiro中创建cookie信息
AbstractNativeSessionManager:
public Session start(SessionContext context) {
Session session = this.createSession(context); //创建simpleSession
this.applyGlobalSessionTimeout(session);
this.onStart(session, context);
this.notifyStart(session);//通知
return this.createExposedSession(session, context);
}
DefaultWebSessionManager:
protected void onStart(Session session, SessionContext context) {
super.onStart(session, context);
if (!WebUtils.isHttp(context)) {
log.debug("SessionContext argument is not HTTP compatible or does not have an HTTP request/response pair. No session ID cookie will be set.");
} else {
HttpServletRequest request = WebUtils.getHttpRequest(context);
HttpServletResponse response = WebUtils.getHttpResponse(context);
if (this.isSessionIdCookieEnabled()) {
Serializable sessionId = session.getId();
this.storeSessionId(sessionId, request, response);//将sessionid保存到cookie
} else {
log.debug("Session ID cookie is disabled. No cookie has been set for new session with id {}", session.getId());
}
request.removeAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE);
request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_IS_NEW, Boolean.TRUE);
}
}