apache deltaspike0.5的入门小例子

seam3已经宣布不再发布新的版本。据seam论坛说,seam的开发成员去了apache deltaspike,所以,开始apache deltaspike的体验。

这个例子是一个简单的登录,与权限无关。


1.环境:

jboss7.1.1.final

jdk1.7

jsf2.1

deltaspike0.5(cdi的扩展)

picketlink2.5.2(安全框架)


2.首先给出一个简单的登录页面。

-----

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">

    <h:messages globalOnly="true"/>

<h:form id="loginForm" rendered="#{not identity.loggedIn}">

    <div class="loginRow">
        <h:outputLabel for="name" value="Username" styleClass="loginLabel"/>
        <h:inputText id="name" value="#{loginCredentials.userId}"/>
    </div>

    <div class="loginRow">
        <h:outputLabel for="password" value="Password" styleClass="loginLabel"/>
        <h:inputSecret id="password" value="#{loginCredentials.password}" redisplay="true"/>
    </div>

    <div class="loginRow">

    </div>

    <div class="buttons">
        <h:commandButton id="login" value="Login" action="#{identity.login}" styleClass="loginButton"/>
    </div>
    <p>demo about jpa and picketlink.</p>

</h:form>

<ui:fragment rendered="#{identity.loggedIn}">
   <h:form>
 Welcome <b>#{identity.account.loginName}</b>!
       <h:commandButton value="Logout" action="#{identity.logout}" />
       <p />

   </h:form>
</ui:fragment>

</html>

-----

3.单击登录按钮{Login}时,将会触发action="#{identity.login}"事件,此事件自动调用BaseAuthenticator类的authenticate方法。


4.重写authenticate方法,用来从用户表中检查登录用户是否是存在于数据库中。

---


package security;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.picketlink.annotations.PicketLink;
import org.picketlink.authentication.BaseAuthenticator;
import org.picketlink.credential.DefaultLoginCredentials;
import org.picketlink.idm.model.basic.User;

@PicketLink
public class SimpleAuthenticator extends BaseAuthenticator {
    @Inject DefaultLoginCredentials credentials;
    @Inject EntityManager em;
    @Override
    public void authenticate() {
        String username,password;
        Query query = null;
        List result=new ArrayList();
        
        username=credentials.getUserId();
        password=credentials.getPassword();
        //EntityManager em=BeanProvider.getContextualReference(EntityManager.class,false);
        String sql="select * from webuser where username=:u and password=:p";

        //我用的是jpa原生Sql,可以用其他的方式。
        query=em.createNativeQuery(sql);
        query.setParameter("u", username);
        query.setParameter("p", password);
        result=query.getResultList();
        if(result.size()>0){

            User u = new User(credentials.getUserId());
            System.out.println(credentials.getUserId() +" logined.");
            // if employee credentials.getUserId() login
            u.setEmail(credentials.getUserId()+"@deltaspike.com");
            u.setFirstName(credentials.getUserId());
            u.setLastName(credentials.getUserId());
            setAccount(u);
            setStatus(AuthenticationStatus.SUCCESS);

        }else{
            setStatus(AuthenticationStatus.FAILURE);
           // FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Authentication Failure - The username or password you provided were invalid."));

        }
    }
}

---




需要说明的:

1.用了maven,所以第一次运行要能够联网,才能下载需要的依赖包。

2.源码在这里http://download.csdn.net/detail/mihaisheng/6530503


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值