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>

扫描二维码关注公众号,回复: 544932 查看本文章

<!-- 下面的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>

 <tr><td>

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

     </td>

   </tr>

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

 </table>

</form> 

 

 

二、开发一个简单自定义认证模块

       在众多Loginmodule中,UsernamePasswordLoginModule是一个可以快速扩展的类,它已经处理了提交进来的参数,我们只需简单扩展就可以使用,代码参考:

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);
    }

    @Override
    protected String getUsersPassword() throws LoginException
    {
        return getUsername();
    }

}

 

三、在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下,重启服务器,

 访问http://xxxx:端口/test  ,输入账号密码,ok,你的jboss自定义认证模块可以使用了。  

 

 

 

 

猜你喜欢

转载自coderbase64.iteye.com/blog/2053942