Posts 关于 tomcat 原生 ldap 集成与 javaee 资源授权
Post
Cancel

关于 tomcat 原生 ldap 集成与 javaee 资源授权

此篇主要是利用JavaEE规范标准涵盖的权限资源管理进行实现,并不涉及Spring Security或Shiro相关范围,需要获取Spring Security与LDAP整合的例子请参考此篇:点击这里,可能会涵盖一些WebSphere的注意点,但WebSphere与LDAP集成的具体配置此篇不会涵盖,因为只需要进到WAS的管理界面配置就好了。

说明

LDAP与Tomcat整合,你需要做几个步骤,首先需要下载一个LDAP服务器进行安装,然后再下载一个LDAP浏览器的客户端进行设置或查看LDAP内容,在这里我主要推荐如下几个软件,具体安全步骤与内容设置我就不一一陈述了;

LDAP Server

  • OpenLDAP for windows

    LDAP Client

  • LDAPBrowser(Java版,不太好用)
  • Apache Directory Studio (开源版,功能强大)http://directory.apache.org/studio/downloads.html

    LDAP Java Libraries

  • UnboundID LDAP SDK for Java

描述

LDAP角色权限控制主要分为资源权限与业务代码权限.

  1. WEB.XML主要用来控制资源权限,限制指定角色访问的资源,参考:WEB.XML篇
  2. 业务代码权限需要手动验证,也就是登陆后从request取出权限进行判断,参考:页面登陆篇

tomcat篇

我们主要采用Tomcat提供的Realm机制对LDAP进行远程地址绑定,采用Tomcat内部提供的登陆机制进行处理;首先找到tomcat/conf/server.xml并打开,找到如下内容:

1
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>

在此段XML内容下加入下列内容:

1
2
3
4
5
6
7
8
9
10
11
<Realm  className="org.apache.catalina.realm.JNDIRealm"
        connectionURL="ldap://172.29.72.182:389"
        connectionName="cn=root"
        connectionPassword="root"
        userBase="ou=people,dc=csvw,dc=com"
        userSearch="(uid={0})"
        userSubtree="false"
        roleBase="cn=les,cn=function,dc=csvw,dc=com"
        roleName="cn"
        roleSearch="(member={0})" 
        roleSubtree="true" />

属性解释说明

  • connectionURL LDAP的远程地址
  • connectionName LDAP管理员用户名
  • connectionPassword LDAP管理员用户密码
  • userBase LDAP用户所属目录,对应的Search DN.
  • userSearch 指定属性匹配,注意:这里{0}表达式与前台输入的登陆名进行匹配.
  • userSubtree 查询的时候是否包含子项,这里会根据userBase指定的目录进行查询所有子项对应的信息.
  • roleBase 角色权限所属目录
  • roleName 最后显示的角色名,这里就是最终的权限名,需要和Web.xml配置的进行匹配.
  • roleSearch 指定属性进行匹配,注意:这里{0}表达式对应的是登陆成功后的用户目录,如:uid=kongqi,ou=people,dc=csvw,dc=com
  • roleSubtree 查询的时候是否包含子项,这里会根据roleBase指定的目录进行查询所有子项对应的信息.

WEB.XML篇

在Tomcat配置LDAP服务器完毕后,需要再项目对应的WEB.XML控制权限资源目录的访问,则需要做以下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<security-constraint>
    <web-resource-collection>
        <web-resource-name>PRODUCTION MANAGEMENT</web-resource-name>
        <url-pattern>/actions/*</url-pattern>
        <url-pattern>/FCL/*</url-pattern>
        <url-pattern>/vp/*</url-pattern>
        <url-pattern>/vdm/*</url-pattern>
        <url-pattern>/print/*</url-pattern>
        <url-pattern>/system/*</url-pattern>
        <url-pattern>/modules/*</url-pattern>
        <url-pattern>/index.jsp</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method> 
    </web-resource-collection>
    <auth-constraint>
        <role-name>权限名称1</role-name>
        <role-name>权限名称2</role-name>
    </auth-constraint>
    <user-data-constraint>
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>  
</security-constraint>
<security-constraint>
    <web-resource-collection>
        <web-resource-name>Admin Console</web-resource-name>
        <url-pattern>/admin/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method> 
    </web-resource-collection>
    <auth-constraint>
        <role-name>权限名称4</role-name>
        <role-name>权限名称5</role-name>
    </auth-constraint>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>  
</security-constraint>
<login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
         <form-login-page>/login.jsp</form-login-page>
         <form-error-page>/error.html</form-error-page>
    </form-login-config>
</login-config> 

注意

  • 里面配置的角色信息代表,只有此类角色才有权限访问里面的资源信息.
  • 里面配置安全等级,取值范围:(NONE, INTEGRAL or CONFIDENTIAL)

页面登陆篇

由于采用tomcat与LDAP绑定以及自动让Tomcat识别权限内容,那么前台页面登陆的写法也是固定的,如下:

1
2
3
4
<form id="login" name="login" method="post" action="j_security_check">
    <input type="hidden" name="j_username" id="username" maxlength="20" />
    <input type="hidden" name="j_password" id="password" maxlength="50" />
</form>

必须的属性值: name=”login” method=”post” action=”j_security_check” name=”j_username” name=”j_password”

另外

登陆成功后所有的角色权限内容都会由服务器自动存储在request里面,可以根据以下代码判断角色权限:

1
2
3
4
5
6
7
public static boolean getRole(HttpServletRequest request, String roleName) {
    if (request.getRemoteUser() != null) {
        return request.isUserInRole(roleName);
    } else {
        return true;
    }
}

页面登出篇一(适用于Websphere)

1
2
3
4
<form method="post" action="ibm_security_logout" name="logout">
    <input type="hidden" name="logout" value="Logout">
    <input type="hidden" name="logoutExitPage" value="/login.jsp">
</form>

必须的属性值: name=”logout” method=”post” action=”ibm_security_logout” name=”logout” name=”logoutExitPage”

页面登出篇二(适用于所有服务器)

1
2
3
HttpSession session = request.getSession();
session.invalidate();
session = request.getSession(true);

设置会话过期之后再重新生成会话!记住每个页面要对原始Session进行过期判断,这样才能满足需求!

J2EE权限验证的会话超时注意点

tomcat当会话超时后会将RemoteUser设置为NULL,并且会把新请求跳转到welcome的配置界面. websphere会话超时后不会将RemoteUser设置为NULL,也不会对新请求做出处理.

相关参考链接

http://tomcat.apache.org/tomcat-6.0-doc/realm-howto.html#JNDIRealm http://ldapwiki.willeke.com/wiki/Tomcat%20And%20LDAP http://pic.dhe.ibm.com/infocenter/wrklight/v6r0m0/index.jsp?topic=%2Fcom.ibm.worklight.help.doc%2Fappcenter%2Fr_ac_tom_ldap.html http://wiki.metawerx.net/wiki/Web.xml.UserDataConstraint http://docs.oracle.com/javaee/5/tutorial/doc/bncav.html http://wiki.metawerx.net/wiki/Web.xml.SecurityRole http://wibiline.iteye.com/blog/655424

This post is licensed under CC BY 4.0

Centos 7 内核升级

Docker 容器与镜像清理汇总

Comments powered by Disqus.