Geronimo3.0中配置默认的security

来源:互联网 发布:js confirm 修改按钮 编辑:程序博客网 时间:2024/06/05 04:11

在Geronimo服务器中,基本的安全配置分成两个插件: j2ee-security和server-security-config。其中jaac provider和keystore manager在j2ee-security中,这部分通常是不需要改的。非常有可能需要改变的在server-secrity-config中。

Realms

Geronimo有四种不同类型的security realms:

  • Certificate Properties File Realm
  • Database (SQL) Realm
  • LDAP Realm
  • Properties File Realm

安全域

Realm相当于一个数据库,这个数据库包括用户名和密码,以及每个合法用法的角色 (相当于group)。

一个安全域可以有一到多个login modules,用于指示授权方式,是通过提交form表单的方式登陆还是其它方式。

Properties File Realm

顾名思义,用户和group都放在properties文件中管理。在geronimo 3.0中默认有一个命名为geronimo-admin的properties file realm。

所有用户和用户组的信息默认放在如下位置:

usersURI=var/security/ users.properties
groupsURI=var/security/ groups.properties

自定义安全域

自定义安全域需要创建自己的类,这个类需要实现org.apache.geronimo.security.realm.Providers (实现了javax.security.auth.api.LoginModule接口)。

另外,还必须在deployment plan文件中(即geronimo-web.xml)定义一个依赖关系。

WEB-INF/web.xml

 web.xml应该包含

  • 1到多个security-constraint 块指定被保护的页面和路径
  • 一个login-config块配置应用程序的登陆方式
  • 1到多个security-role 块,列出被应用程序使用的安全角色

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"         version="2.4">   <!--  servlets and mappings and normal web.xmlstuff here -->    <security-constraint>       <web-resource-collection>           <web-resource-name>Protected</web-resource-name>           <url-pattern>/protected/*</url-pattern>           <http-method>GET</http-method>           <http-method>POST</http-method>       </web-resource-collection>       <auth-constraint>           <role-name>admin</role-name>       </auth-constraint>   </security-constraint>    <login-config>       <auth-method>FORM</auth-method>       <realm-name>This is not used for FORM login</realm-name>       <form-login-config>           <form-login-page>/login.jsp</form-login-page>           <form-error-page>/loginerror.jsp</form-error-page>     </form-login-config>    </login-config>    <security-role>        <role-name>admin</role-name>   </security-role></web-app>

WEB-INF/geronimo-web.xml (Geronimo 3.0为例)

为每人角色配置安全域和角色成员 (security realm and the members of each role), 这些写在geronimo-web.xml部署计划中,这个文件一般放在WEB-INF下边。

geronimo-web.xml应该有一个security-realm-name元素,指明使用哪个安全域(realm)用于授权登陆。同时,应该还有一个一个security元素列出web.xm中使用的每个安全角色的用户和组(users and groups of a security-role)。

需要注意,一个角色可以有0到多个用户组和0到多个用户(甚至有其中一个),一个用户组可以有0到多个用户。因此用户组和角色是两个不同的概念。

<?xml version="1.0" encoding="UTF-8"?><web:web-appxmlns:app="http://geronimo.apache.org/xml/ns/j2ee/application-2.0"xmlns:bp="http://www.osgi.org/xmlns/blueprint/v1.0.0"xmlns:client="http://geronimo.apache.org/xml/ns/j2ee/application-client-2.0"xmlns:conn="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2"xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2"xmlns:ejb="http://openejb.apache.org/xml/ns/openejb-jar-2.2"xmlns:jaspi="http://geronimo.apache.org/xml/ns/geronimo-jaspi"xmlns:log="http://geronimo.apache.org/xml/ns/loginconfig-2.0"xmlns:name="http://geronimo.apache.org/xml/ns/naming-1.2"xmlns:pers="http://java.sun.com/xml/ns/persistence"xmlns:pkgen="http://openejb.apache.org/xml/ns/pkgen-2.1"xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" xmlns:web="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1">   <dep:environment>       <dep:moduleId>           <dep:groupId>default</dep:groupId>           <dep:artifactId>JSPResearch</dep:artifactId>           <dep:version>1.0</dep:version>            <dep:type>car</dep:type>       </dep:moduleId>         </dep:environment>      <web:context-root>/JSPResearch</web:context-root>       <web:security-realm-name>geronimo-admin</web:security-realm-name>     <sec:securitydefault-role="admin">       <sec:role-mappings>            <sec:rolerole-name="admin">                <sec:principalclass="org.apache.geronimo.security.realm.providers.GeronimoGroupPrincipal"name="admin"/>                <sec:principalclass="org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal"name="linus"/>           </sec:role>       </sec:role-mappings>    </sec:security></web:web-app>

以上这个例子以admin group和用户linus作为admin角色。因为使用的是geronimo-admin默认realm,因此这些group和users在console中的users and groups页面应该可以被看到。