Shiro - web应用

先不谈Spring,首先试着用最简易的方式将Shiro集成到web应用。 即使用一些Servlet ContextListener、Filter、ini这些简单的配置完成与web应用的集成。

web.xml:

<listener>
    <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>shiroEnvironmentClass</param-name>
    <param-value>org.apache.shiro.web.env.IniWebEnvironment</param-value>
</context-param>
<context-param>
    <param-name>shiroConfigLocations</param-name>
    <param-value>classpath:shiro_web.ini</param-value>
</context-param>

上面的配置中我注册了一个Listener——org.apache.shiro.web.env.EnvironmentLoaderListener。
该类的意义主要是为了实现ServletContextListener,将WebEnvironment随着ServletContext事件进行创建和销毁。
对WebEnvironment的处理逻辑全部在其父类——EnvironmentLoader中。

WebEnvironment的类关系图:

如果想获取WebEnvironment则可以试试以下方法:

WebUtils.getRequiredWebEnvironment(servletContext);

上面的配置中用到了两个参数(事实上EnvironmentLoader也只有这两个参数)。

  • shiroEnvironmentClass
  • shiroConfigLocations

shiroEnvironmentClass用于指定使用的WebEnvironment实现类,缺省值为org.apache.shiro.web.env.IniWebEnvironment。
IniWebEnvironment根据设置的.ini配置文件的路径创建ini实例,如果无法获得.ini配置文件则抛出ConfigurationException。

当然,如果有需要(比如换个配置格式、解析方法什么的...),我们也可以自己实现一个WebEnvirontment,并通过shiroEnvironmentClass属性来进行注册。

而shiroConfigLocations则是指定.ini配置文件的路径的参数。
如果没有进行手动指定,他会尝试在以下两个路径中寻找:

public static final String DEFAULT_WEB_INI_RESOURCE_PATH = "/WEB-INF/shiro.ini";
public static final String DEFAULT_INI_RESOURCE_PATH = "classpath:shiro.ini";

顺便记录,IniWebEnvironment查找.ini配置时使用ResourceUtils,见:

private Ini convertPathToIni(String path, boolean required) {

    //TODO - this logic is ugly - it‘d be ideal if we had a Resource API to polymorphically encaspulate this behavior

    Ini ini = null;

    if (StringUtils.hasText(path)) {
        InputStream is = null;

        //SHIRO-178: Check for servlet context resource and not only resource paths:
        if (!ResourceUtils.hasResourcePrefix(path)) {
            is = getServletContextResourceStream(path);
        } else {
            try {
                is = ResourceUtils.getInputStreamForPath(path);
            } catch (IOException e) {
                if (required) {
                    throw new ConfigurationException(e);
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("Unable to load optional path ‘" + path + "‘.", e);
                    }
                }
            }
        }
        if (is != null) {
            ini = new Ini();
            ini.load(is);
        } else {
            if (required) {
                throw new ConfigurationException("Unable to load resource path ‘" + path + "‘");
            }
        }
    }

    return ini;
}

该方法首先调用ResourceUtils.hasResourcePrefix(path)检查路径前缀是否符合以下三种之一:

public static final String CLASSPATH_PREFIX = "classpath:";
public static final String URL_PREFIX = "url:";
public static final String FILE_PREFIX = "file:";

如果不符合这三种前缀则在Servlet Context进行查找。

如果符合三种前缀之一,则调用ResourceUtils.getInputStreamForPath(path),根据path及其不同的前缀以不同的方式获取输入流。

对于classpath,调用ClassUtils.getResourceAsStream(path);,通过ClassLoader实例调用getResourceAsStream(name);

对于url,则是返回url.openStream();

对于file,返回new FileInputStream(path);

继续配置web.xml,这次添加一个Filter:

<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>
    <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
            <dispatcher>ERROR</dispatcher>
    </filter-mapping>

这是基于当前的WebEnvironment实例配置的Filter,即单独存在没什么意义。

ShiroFilter用WebEnvironment实例对所有被过滤的请求进行安全处理。

Shiro提供的一些Filter实现:

暂且不论AdviceFilter,我们使用的ShiroFilter在AbstractShiroFilter下。

其中IniShiroFilter从1.2开始已deprecated了,但这个东西用起来还是有点意思的,只不过没什么意义。

IniShiroFilter不需要同时配置EnvironmentLoaderListener,也就是说这里面没有WebEnvironment对象,他本身就是一个简易的Environment。

有意思的地方就是这点,他可以把.ini中的配置直接写到web.xml,比如这样:

<filter>
    <filter-name>ShiroFilter</filter-name>
    <filter-class>org.apache.shiro.web.servlet.IniShiroFilter</filter-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>
        [urls]
        /main/logout = logout
        /main/loginPage = anon
        /** = user
        [main]
        user.loginUrl = /main/login
        authc.successUrl = /main/welcome
        myRealm=pac.king.common.security.realm.MainRealm
        securityManager.realms=$myRealm
        </param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>ShiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

有意思,但没什么意义。

Shiro也建议用户们不要这样配置,对此他们给出了几个理由:

  • 安全配置可能会经常变化,而我们不想总是修改web.xml。
  • 安全配置可能会越来越庞大,这会影响web.xml的可读性。
  • 我们尽量保证安全配置不会散落在各个地方。

无论如何,这取决于用户和项目。

另外说说web应用相关的ini配置。

之前几篇中用过[main]、[users]、[roles]等片段,在web应用中我们可以试试[urls]。

[urls]也是Shiro的一大卖点(文档提供人说根本没见过其他web framework也能做到这点)。

就是为每个URL配置专有的filter chain!!

[urls]的格式如下:

URL_Ant_Path_Expression = Path_Specific_Filter_Chain[optional_config]

左侧使用Ant风格的表达式描述URL;

右侧则是用逗号分隔的过滤器链;

最后的optional_config则是一些附加属性,比如描述对用户资源有删除操作的权限perms["user:delete"]。

配置[urls],官网上的例子:

[urls]
/index.html = anon
/user/create = anon
/user/** = authc
/admin/** = authc, roles[administrator]
/rest/** = authc, rest
/remoting/rpc/** = authc, perms["remote:invoke"]

URL是相对路径,即使部署的时候换了个域名也没有问题。

注意!URL配置的顺序对filter chain是有影响的!他是FIRST MATCH WINS。

比如下面的例子中,第二行配置就不会生效。

/user/** = authc
/user/list = anon

默认的Filter,比如anon,authc,users等等,他们是由哪些类来实现的?

Filter Name Class
anon org.apache.shiro.web.filter.authc.AnonymousFilter
authc org.apache.shiro.web.filter.authc.FormAuthenticationFilter
authcBasic org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
logout org.apache.shiro.web.filter.authc.LogoutFilter
noSessionCreation org.apache.shiro.web.filter.session.NoSessionCreationFilter
perms org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
port org.apache.shiro.web.filter.authz.PortFilter
rest org.apache.shiro.web.filter.authz.HttpMethodPermissionFilterv
roles org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
ssl org.apache.shiro.web.filter.authz.SslFilter
user org.apache.shiro.web.filter.authc.UserFilter

应用启动时,将默认Filter全部加载。

默认Filter的定义见enum类DefaultFilter。

pool of Filters则定义在DefaultFilterChainManager中,用LinkedHashMap维护。DefaultFilterChainManager在constructor中调用void addDefaultFilters(boolean init)将Filters放入Map中。

随着应用做得越来越大,这些URL会变得越来越难以管理。

当然,我们也可以把这些URL放在数据库里管理,但总是有个别的URL需要特殊配置Filter。

就以我现在工作中的应用为例,我们将大多数URL放到数据库管理,并全部采用authc+perms过滤器,perms的options也是该URL。

即有该URL权限的用户可以访问该URL。

但总有那么些例外的、奇怪的东西,除了数据库里的URL,我又在.ini中写了差不多30个URL,再配上各种各样的Filter,当然还要注意顺序的影响。

随着开发、测试、生产环境的切换,这些filters也需要可以进行启用/禁用。

我总不把filter能一个个删掉再一个个写回去...

见OncePerRequestFilter有个field:

private boolean enabled = true;

而且所有的default filters都继承了OncePerRequestFilter!!

于是我可以直接在ini文件中直接进行启用/禁用,比如这样:

user.enabled=false

当然,我们也可以试着自定义一个Filter(比如根据判断具体的请求或者路径,动态将所有filter启用/禁用),并在[main]注册。

另外,上面的类关系图中AccessControlFilter有一个field为loginUrl,其默认值为:

public static final String DEFAULT_LOGIN_URL = "/login.jsp";

我们经常使用的filter中的authc(FormAuthenticationFilter)中存在以下属性:

public static final String DEFAULT_USERNAME_PARAM = "username";
public static final String DEFAULT_PASSWORD_PARAM = "password";
public static final String DEFAULT_REMEMBER_ME_PARAM = "rememberMe";

private String usernameParam = DEFAULT_USERNAME_PARAM;
private String passwordParam = DEFAULT_PASSWORD_PARAM;
private String rememberMeParam = DEFAULT_REMEMBER_ME_PARAM;

我们可以在表单中使用这些属性,让其进行认证+remember me。

当然,这些值也是可以改变的,比如:

[main]authc.loginUrl = /main/loginauthc.usernameParam = userNameauthc.passwordParam = pwdauthc.rememberMeParam = rememberCookie

说到remember me,其实现是有RememberMeManager提供,默认实现是基于Cookie的。

比如DefaultWebSecurityManager的constructor中将CookieRememberMeManager设为默认(field定义于其父类DefaultSecurityManager):

public DefaultWebSecurityManager() {
    super();
    ((DefaultSubjectDAO) this.subjectDAO).setSessionStorageEvaluator(new DefaultWebSessionStorageEvaluator());
    this.sessionMode = HTTP_SESSION_MODE;
    setSubjectFactory(new DefaultWebSubjectFactory());
    setRememberMeManager(new CookieRememberMeManager());
    setSessionManager(new ServletContainerSessionManager());
}

看起来不错,那我就一步步detect看看RememberMeManager是怎么manage的。

用户登录时我们调用Subject.login(token)
以DelegaingSubject为例,第一步直接将验证工作委托给securityManager。

工作中一步步进行委托,securityManager -> authenticator -> realm...

验证通过后将AuthenticationInfo结果返回到securityManager,securityManager将结果传递给RememberMeManager,委托rememberMe的工作。

参考AbstractRememberMeManager中的method:

public void onSuccessfulLogin(Subject subject, AuthenticationToken token, AuthenticationInfo info) {
    //always clear any previous identity:
    forgetIdentity(subject);

    //now save the new identity:
    if (isRememberMe(token)) {
        rememberIdentity(subject, token, info);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("AuthenticationToken did not indicate RememberMe is requested.  " +
                    "RememberMe functionality will not be executed for corresponding account.");
        }
    }
}

第一步:先将Cookie移除,Shiro默认使用的Cookie是自己的SimpleCookie,调用其removeFrom方法将Cookie"移除"。

第二步:检查token是否是RememberMeAuthenticationToken的实例并是否设置了rememberMe=true。

第三步:进行rememberMe的具体工作,这个工作由AbstractRememberMeManager的子类进行。

以CookieRememberMeManager为例:

protected void rememberSerializedIdentity(Subject subject, byte[] serialized) {

    if (!WebUtils.isHttp(subject)) {
        if (log.isDebugEnabled()) {
            String msg = "Subject argument is not an HTTP-aware instance.  This is required to obtain a servlet " +
                    "request and response in order to set the rememberMe cookie. Returning immediately and " +
                    "ignoring rememberMe operation.";
            log.debug(msg);
        }
        return;
    }

    HttpServletRequest request = WebUtils.getHttpRequest(subject);
    HttpServletResponse response = WebUtils.getHttpResponse(subject);

    //base 64 encode it and store as a cookie:
    String base64 = Base64.encodeToString(serialized);

    Cookie template = getCookie(); //the class attribute is really a template for the outgoing cookies
    Cookie cookie = new SimpleCookie(template);
    cookie.setValue(base64);
    cookie.saveTo(request, response);
}

代码非常简单,接着转到SimpleCookie:

public void saveTo(HttpServletRequest request, HttpServletResponse response) {

    String name = getName();
    String value = getValue();
    String comment = getComment();
    String domain = getDomain();
    String path = calculatePath(request);
    int maxAge = getMaxAge();
    int version = getVersion();
    boolean secure = isSecure();
    boolean httpOnly = isHttpOnly();

    addCookieHeader(response, name, value, comment, domain, path, maxAge, version, secure, httpOnly);
}

private void addCookieHeader(HttpServletResponse response, String name, String value, String comment,
                             String domain, String path, int maxAge, int version,
                             boolean secure, boolean httpOnly) {

    String headerValue = buildHeaderValue(name, value, comment, domain, path, maxAge, version, secure, httpOnly);
    response.addHeader(COOKIE_HEADER_NAME, headerValue);

    if (log.isDebugEnabled()) {
        log.debug("Added HttpServletResponse Cookie [{}]", headerValue);
    }
}

但毕竟很多人不喜欢cookie...我们也可以自己去实现RememberMeManager,并进行注册(仍然是注入到securityManger):

rememberMeManager = com.my.impl.RememberMeManager
securityManager.rememberMeManager = $rememberMeManager

我们使用的UsernamePasswordToken继承的RememberMeAuthenticationToken提供rememberMe特性。

boolean isRememberMe();

比如我们可以Realm的验证方法中这样使用:

UsernamePasswordToken uToken = (UsernamePasswordToken)token;
uToken.setRememberMe(true);
时间: 2024-08-09 11:02:03

Shiro - web应用的相关文章

org.apache.shiro.web.servlet.ShiroHttpServletRequest cannot be cast to org.springframwork.web.mult..

在用dwz框架+spring MVC时上传文件报的错 具体错误如下: 其实就是一个类型转换错误,但却研究了好长时间,怎么都不知道哪里错了,因为前面卸过一个文件上传的和这个差不多,那个就没有问题. 问题解决:其实包这个错误有两个原因:一个是表单的属性,有文件上传是,表单的类型一定要是formdata类型-----我当然不是犯的这个错误. 但后来用断点调试的时候,发现后台接收的类型不是formdata类型的,这就奇怪了.难道是这个表单和其他的东西有什么莫名的联系??好了, 大晚上的也不兜圈子了,困了

Shiro Shiro Web Support and EnvironmentLoaderListener

Shiro Shiro Web Support 主要参考: http://shiro.apache.org/web.html 还有涛哥的 作为资源控制访问的事情,主要使用在网络后台方面,所以了解了本地的测试之后,了解web方面的还是比较的很有必要的,本文主要讲解如何简单的定义一个web项目,然后简单的了解实现的方式是怎么样的! 配置 shiro.ini 放置在 resource目录下面至于这些配置的到底是什么意思,这个不是我们关注的重点,随便看看就知道了,一会慢慢的深入了就知道了这个到底是啥子意

Apache Shiro Web应用整合-配置

博客分类: Shiro Shiro 将Shiro  集成到任何 Web  应用程序的最简单的方法是在 web.xml  中配置 ContextListener  和 Filter ,来使 Shiro 知道如何读取 Shiro 的 INI  配置文件. 注意:Spring  框架用户将不执行此设置.如果你使用 Spring ,你将要阅读关于 Spring  特定的 Web  配置. Web.xml Shiro 1.2 and later 在Shiro 1.2  及以后版本,标准的 Web  应用程

Shiro Web集成及拦截器机制

Shiro与 Web 集成 Shiro 提供了与 Web 集成的支持,其通过一个 ShiroFilter 入口来拦截需要安全控制的 URL,然后进行相应的控制,ShiroFilter 类似于如 Strut2/SpringMVC 这种 web 框架的前端控制器,其是安全控制的入口点,其负责读取配置(如 ini 配置文件),然后判断 URL 是否需要登录 / 权限等工作. ShiroFilter 入口 Shiro 1.2 及以后版本的配置方式 从 Shiro 1.2 开始引入了 Environmen

spring+springMVC+mybatis+shiro -- 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 htt

在 Web 项目中应用 Apache Shiro

Apache Shiro 是功能强大并且容易集成的开源权限框架,它能够完成认证.授权.加密.会话管理等功能.认证和授权为权限控制的核心,简单来说,"认证"就是证明你是谁? Web 应用程序一般做法通过表单提交用户名及密码达到认证目的."授权"即是否允许已认证用户访问受保护资源.关于 Shiro 的一系列特征及优点,很多文章已有列举,这里不再逐一赘述,本文重点介绍 Shiro 在 Web Application 中如何实现验证码认证以及如何实现单点登录. 用户权限模型

web工程使用spring mvc+shiro进行权限控制

第1步:引入shiro相关jar包 ehcache-core-2.5.0.jar shiro-ehcache-1.2.3.jar shiro-core-1.2.3.jar shiro-web-1.2.3.jar shiro-spring-1.2.3.jar 第二步:web.xml配置 <!-- shiro的filter --> <!-- shiro过虑器,DelegatingFilterProxy通过代理模式将spring容器中的bean和filter关联起来 --> <fi

基于Spring + Spring MVC + Mybatis + shiro 高性能web构建

一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJS,做了大量的研究,对前后端交互有了更深层次的认识. 今天抽个时间写这篇文章,我有预感,这将是一篇很详细的文章,详细的配置,详细的注释,看起来应该很容易懂. 用最合适的技术去实现,并不断追求最佳实践.这就是架构之道. 希望这篇文章能给你们带来一些帮助,同时希望你们可以为这个项目贡献你的想法. 源码地址:https://github.com/starzou/quick4j 点击打开 看我们的项目结构: 是一个典型

Web项目Shiro总结及源码(十六)

shiro过虑器 过滤器简称 对应的java类 anon org.apache.shiro.web.filter.authc.AnonymousFilter authc org.apache.shiro.web.filter.authc.FormAuthenticationFilter authcBasic org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter perms org.apache.shiro.web.filt