1.认证和授权概述

(1)认证:对用户的身份进行验证。

.NET基于的RBS(参考1)的认证和授权相关的核心是2个接口System.Security.Principal.IPrincipal和System.Security.Principal.IIdentity。我们自己实现认证过程,通过Thread.CurrentPrincipal来设置和读取认证结果。认证成功后设置认证状态和标识。

Java内置了的JAAS(参考2),核心是javax.security.auth.Subject类和javax.security.Principal接口。java对认证过程也提供了2个类型,javax.security.auth.login.LoginContext类和javax.security.auth.spi.LoginModule接口。我们自己实现认证过程,但只要实现了LoginModule就可以通过LoginContext使用一致的语法。

(2)授权:对用户的权限进行验证,通常使用Role(角色)管理权限。

.NET的支持基于角色的授权。.NET的IPrincipal接口的IsInRole方法是授权的核心。有两种方式使用:1.使用内置的IPrincipal对象(如GenericIdentity),在认证的同时加载用户的角色roles。2.自定义IPrincipal实现,实现自己的IsInRole逻辑。ASP.NET中实现的的RolePrincipal就通过将逻辑转发给System.Web.Security.Roles静态类。Roles依赖System.Web.Security.RoleProvider接口实现角色的查询,我们可以通过web.config的相关节点来配置自定义的RoleProvider。

JAAS的IPrincipal接口没有提供IsInRole方法,我们有2个选择,要么通过多个IPrincipal表示角色,要么自定义实现IPrincipal添加角色支持。Tomcat容器实现的org.apache.catalina.realm.GenericPrincipal就和.NET中的System.Security.Principal.GenericIdentity十分类似的角色实现。

Java的Subject类和IPrincipal接口与.NET的IPrincipal接口和IIdentity的接口不容易对应。为了便于统一理解.NET和Java的核心类型,我们可以从成员的理解,可以认为Java的Principal类型相当于.NET中的IPrincipa和IIdentity两个类型的作用。Subject只是作为Principal的聚合根。之前提到的Tomcat容器中的GenericPrincipal就即提供了hasRole和getName成员。Tomcat实现的HttpServletRequest对应的成员就是通过GenericPrincipal实现的。

# 成员 .NET Java
认证类型 AuthenticationType System.Security.Principal.IIdentity.AuthenticationType javax.servlet.http.HttpServletRequest.getAuthType()
标识名称 Name System.Security.Principal.IIdentity.Name java.security.Principal.getName()
角色验证 IsInRole System.Security.Principal.IPrincipal.IsInRole javax.servlet.http.HttpServletRequest.isUserInRole()

 

2..NET Web认证和授权

ASP.NET Forms认证主采用RolePrincipal主体,未认证用户设置为GenericIdentity标识,已认证用户设置为FormsIdentity。RolePrincipal在验证角色时将使用Roles静态类通过RoleProvider进行角色验证。通过HttpContext.User可以直接调用主体。

实现一个最简单的自定义RoleProvider只需要继承并实现GetRolesForUser和IsUserInRole两个方法,通常可以使用委托在Application_Start中注入的方式实现通用的RoleProvider。

ASP.NET的forms验证通过FormsAuthentication发送和注销用于认证的token,通过配置web.config可以让不同的Web服务器以相同的方式对token加密和解密以适应Web服务器负载均衡。不适用cookie承载token时,可以自定义认证逻辑,比如通过url参数方式承载token配合ssl用于app客户端验证等。

.NET的认证和授权示意图:
Java Web系列:JAAS认证和授权基础

自定义RoleProvider的示例,省略了不需要实现的部分代码,GetRolesForUserDelegate和IsUserInRoleDelegate在Application_Start中注入即可彻底实现RoleProvider和应用服务代码的解耦:

    public class SimpleRoleProvider : RoleProvider
    {
        public static Func<string, string[]> GetRolesForUserDelegate;

        public static Func<string, string, bool> IsUserInRoleDelegate;

        public override string[] GetRolesForUser(string username)
        {
            return GetRolesForUserDelegate(username);
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            return IsUserInRoleDelegate(username, roleName);
        }
    }

Forms身份验证和RoleProvider的分别定义在web.config配置文件中。ASP.NET的配置文件示例(省略了其他配置):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <authentication mode="Forms">
      <forms loginUrl="~/Home/Login" cookieless="UseCookies" slidingExpiration="true" />
    </authentication>
    <roleManager defaultProvider="SimpleRoleProvider" enabled="true">
      <providers>
        <clear />
        <add name="SimpleRoleProvider" type="Onion.Web.SimpleRoleProvider" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

.NET中还有用于配置Pricipal的两个方法System.AppDomain.SetThreadPrincipal和System.AppDomain.SetPrincipalPolicy以及控制访问的两个类型System.Security.Permissions.PrincipalPermission和System.Security.Permissions.PrincipalPermissionAttribute

3.JAAS

HttpServletRequest接口定义了6个验证和授权相关的方法getAuthType()、login()、logout()、getRemoteUser()、isUserInRole()、getUserPrincipal()。类似ASP.NET,Forms身份验证也在配置文件中进行配置。但由于Java热衷于定义一堆接口将实现推迟到容器级别,LoginContext依赖的具体的LoginModule的配置也必须在容器中进行配置。因此除了web.xml,还需要配置在容器中配置JAAS的配置文件。JAAS的示意图:
Java Web系列:JAAS认证和授权基础

(1)JAAS 内置的登录模块使用:NTLoginModule:

配置:

first{
    com.sun.security.auth.module.NTLoginModule Required debug=true;
};

代码

package com.test.jaas1;

import java.security.Principal;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

public class App {
    public static void main(String[] args) {
        System.setProperty("java.security.auth.login.config",
                Thread.currentThread().getContextClassLoader().getResource("jaas.config").getPath());
        try {
            LoginContext lc = new LoginContext("first");
            lc.login();
            System.out.println(lc.getSubject().getPrincipals().size());
            for (Principal item : lc.getSubject().getPrincipals()) {
                System.out.println(String.format("%s principal:%s", item.getClass().getTypeName(), item.getName()));
            }
            lc.logout();
            System.out.println(lc.getSubject().getPrincipals().size());
        } catch (LoginException e) {
            e.printStackTrace();
        }
    }
}

 (2)JAAS Tomcat容器下的登录模块使用(参考3):

用于配置Forms认证:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <display-name>Archetype Created Web Application</display-name>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Admin</web-resource-name>
            <url-pattern>/admin/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>admin</role-name>
        </auth-constraint>
    </security-constraint>

    <security-role>
        <role-name>admin</role-name>
    </security-role>
    <login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
            <form-login-page>/login.html</form-login-page>
            <form-error-page>/error.html</form-error-page>
        </form-login-config>
    </login-config>
</web-app>
View Code

相关文章: