天天看點

JAAS之快速開發JBOSS自定義認證

      在企業應用中,認證是一個很常見的需求,而在j2ee項目中,認證登入大緻有兩種方式來實作:

      一種是通過過濾器來攔截請求控制登入與權限,另外一種方式就是适用jaas, 今天就簡單介紹下使用jaas快速開發一個jboss的自定義認證。

一、環境準備工作:

1.1 一個部署的war包,包内應當配置資源保護,和啟用jaas驗證。

  web-inf中web.xml 中配置資源保護 示例:        

  <security-constraint>

  <web-resource-collection>

   <web-resource-name>war_all_pages</web-resource-name>

   <url-pattern>*.do</url-pattern>

   <url-pattern>*.jsp</url-pattern>

   <url-pattern>*.htm</url-pattern>

   <http-method>get</http-method>

   <http-method>post</http-method>

  </web-resource-collection>

  <auth-constraint>

   <role-name>users</role-name>

  </auth-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.jsp</form-error-page>

  </form-login-config>

 </login-config>

 <security-role>

  <role-name>users</role-name>

 </security-role>

  web-inf 中 jboss-web.xml配置  示例:

<jboss-web>

<!-- 下面的test就是在login-config.xml配置的application-policy的名稱 -->

       <security-domain>java:/jaas/ test</security-domain>

        </jboss-web>

登陸頁面 示例:

根目錄下添加login.jsp ,送出至j_security_check 且送出帳号密碼為j_username 、j_password

<form name="form" method="post" action="j_security_check">

<table >

 <tr><td>

<input type="text" name="j_username" >

 </td> </tr>

<input type="password" name="j_password" >

     </td>

   </tr>

<tr><td></td><td><input type="submit" value="登入"></td></tr>

 </table>

</form> 

二、開發一個簡單自定義認證子產品:

       在衆多loginmodule中,usernamepasswordloginmodule是一個可以快速擴充的類,它已經處理了送出進來的參數,我們隻需簡單擴充就可以使用,代碼參考:

JAAS之快速開發JBOSS自定義認證

<span>public class testloginmodule extends usernamepasswordloginmodule  

{  

    private simpleprincipal user;  

    private boolean guestonly;  

    protected static logger log = logger.getlogger(testloginmodule .class);  

    protected principal getidentity()  

    {  

        principal principal = this.user;  

        if (principal == null)  

            principal = super.getidentity();  

        return principal;  

    }  

    protected boolean validatepassword(string inputpassword, string expectedpassword)  

        boolean isvalid = false;  

        if (inputpassword == null)  

        {  

            this.guestonly = true;  

            isvalid = true;  

            this.user = new simpleprincipal("guest");  

        }  

        else  

            log.debug("inputpassword" + inputpassword);  

             //這裡實作了對使用者密碼的驗證,可以自定義驗證方式。  

            isvalid = inputpassword.equals("aaaaa888");  

        return isvalid;  

    protected group[] getrolesets() throws loginexception  

        group[] rolesets = { new simplegroup("roles") };  

        if (!this.guestonly)  

         //這裡加入了需要的角色。  

            rolesets[0].addmember(new simpleprincipal("users"));  

        rolesets[0].addmember(new simpleprincipal("guest"));  

        return rolesets;  

    @override  

    public void initialize(subject subject, callbackhandler callbackhandler, map sharedstate, map options)  

        super.initialize(subject, callbackhandler, sharedstate, options);  

    protected string getuserspassword() throws loginexception  

        return getusername();  

}</span>  

三、在jboss中配置自定義認證子產品:

         現在需要配置我們開發的認證子產品了,在{jboss_server}\default\conf 下修改login-config.xml

         在    <application-policy name = "other">節點下加入:

          <authentication>

             <login-module code = "org.jboss.security.auth.spi.testloginmodule" flag = "required"></login-module>

           </authentication>

四、測試。

         現在把第一步準備的war包放入部署目錄,把第二步開發的子產品編譯的jar包放入{jboss_server}\default\lib下,重新開機伺服器,