1

我用纯 jax-rs 前端界面创建了我的第一个 Java EE 7 应用程序。一切都经过测试并且工作正常。现在我想应用一个安全层(基于角色,应用于资源或方法)。我对最终想要的东西有清晰的想象(不知道这是否有意义),但我不确定如何到达那里(使用什么成分,按什么顺序)。所以这就是我想要的:

  • 我想使用 wildfly 作为身份提供者(wildfly 应该存储用户凭据 - 在数据库中,加密)
  • 我的应用程序完全基于休息,所以我需要一些方法将身份验证信息放入请求(令牌?!)
  • 基本身份验证就可以(每个用户都必须经过身份验证),不需要表单,不需要自我注册
  • 限制对某些资源/方法的访问,我想使用 Java EE 7 标准(注释、拦截器......)

这有意义吗?如果是,是否有以相同方式或至少非常相似的示例或文档?我找到了jboss-picketlink-quickstarts但这包含许多示例,我不确定哪个最适合。我需要纠察链接吗?

由于我的持久层中有一个带有“UserRole”(枚举)的“用户”,我认为我需要从 IDP(Wildfly)和我自己提供的角色中进行某种映射 - 对吧?

4

1 回答 1

2

这或多或少是我所做的:

  • 用户数据库领域,在standalone.xml 上:

            <security-domain name="THE_Realm" cache-type="default">
                <authentication>
                    <login-module code="Database" flag="required">
                        <module-option name="dsJndiName" value="java:/jdbc/risk_ds"/>
                        <!--module-option name="principalsQuery" value="SELECT encode(pass, 'hex') as 'Password' FROM user WHERE username = ?"/-->
                        <module-option name="principalsQuery" value="SELECT pass as 'Password' FROM user WHERE username = ?"/>
                        <module-option name="rolesQuery" value="select role as 'Role', 'Roles' from user_role WHERE username = ?"/>
                        <module-option name="hashAlgorithm" value="SHA-256"/>
                        <module-option name="hashEncoding" value="hex"/>
                    </login-module>
                </authentication>
            </security-domain>
    
  • 请参阅 log,standalone.xml 上的数据库登录查询:

        <logger category="org.jboss.security">
            <level name="TRACE"/>
        </logger>
    
  • 在数据库上插入密码:设置 SHA256 + HEX 还需要在角色上插入。

    insert into user (username, password) 
        set ('the_name', sha2('the_password',256))
    
  • 在 jboss-web.xml 上设置领域

       <?xml version="1.0" encoding="UTF-8"?>
       <jboss-web>
           <security-domain>THE_Realm</security-domain>
       </jboss-web>
    
  • 在 web.xml 上,创建安全约束:

        <security-constraint>
            <web-resource-collection>
                <web-resource-name>Secure Content</web-resource-name>
                <url-pattern>/the_path/*</url-pattern>
            </web-resource-collection>
            <auth-constraint>
                <role-name>ROLE_USER</role-name>
            </auth-constraint>
        </security-constraint>
    
        <login-config>
            <auth-method>BASIC</auth-method>
            <realm-name>THE_Realm</realm-name>
         </login-config>
    
    
        <security-role>
            <description>The role required to access restricted content </description>
            <role-name>ROLE_USER</role-name>
        </security-role>
    
于 2015-05-24T23:38:10.697 回答