首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

seam3 登陆应验

2012-08-28 
seam3 登陆验证seam3 登陆验证(Seam3 Booking example)1.在pom.xml添加依赖dependencygroupIdorg.jbos

seam3 登陆验证
seam3 登陆验证(Seam3 Booking example)
1.在pom.xml添加依赖

<dependency>           <groupId>org.jboss.seam.security</groupId>           <artifactId>seam-security</artifactId>            <version>3.0.0.Final</version>        </dependency>


2.页面home.xhtml
<h:form id="login" rendered="#{not identity.loggedIn}">         <fieldset>            <div>               <h:outputLabel for="username" value="#{bundles.messages.home_usernameLabel}"/>               [color=red]<[/color]               <div value="#{bundles.messages.home_passwordLabel}"/>              [color=red]<h:inputSecret id="password" value="#{credentials.password}" style="width: 175px;"/>[/color]             </div>            <span styleid="messages" globalOnly="true"/>            </span>            <div action="#{identity.login}" value="#{bundles.messages.home_loginAction}"/>[/color]</div>            <div outcome="/register.xhtml" value="#{bundles.messages.home_registerAction}"/></div>            <div name="code"><?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:ee="urn:java:ee" xmlns:ss="urn:java:org.jboss.seam.security"    xsi:schemaLocation="        http://java.sun.com/xml/ns/javaee         http://docs.jboss.org/cdi/beans_1_0.xsd">    <ss:IdentityImpl>    <ee:modifies />    <ss:authenticatorName>bookingAuthenticator</ss:authenticatorName>    </ss:IdentityImpl>    </beans>

4.实现验证的BookingAuthenticator类,实现先Authenticator接口,重写authenticate()
@Stateless@Named("bookingAuthenticator")public class BookingAuthenticator extends BaseAuthenticator implements Authenticator {    @Inject    private Logger log;    @PersistenceContext    private EntityManager em;    @Inject    private Credentials credentials;    @Inject    private Messages messages;    @Inject    @Authenticated    private Event<User> loginEventSrc;    public void authenticate() {        log.info("Logging in " + credentials.getUsername());        if ((credentials.getUsername() == null) || (credentials.getCredential() == null)) {            messages.error(new DefaultBundleKey("identity_loginFailed")).defaults("Invalid username or password");            setStatus(AuthenticationStatus.FAILURE);        }        User user = em.find(User.class, credentials.getUsername());        if (user != null && credentials.getCredential() instanceof PasswordCredential){        if(user.getPassword().equals(((PasswordCredential) credentials.getCredential()).getValue())) {            loginEventSrc.fire(user);            messages.info(new DefaultBundleKey("identity_loggedIn"), user.getName()).defaults("You're signed in as {0}")                    .params(user.getName());            setStatus(AuthenticationStatus.SUCCESS);            setUser(new SimpleUser(user.getUsername())); //TODO confirm the need for this set method            return;        }        }                     messages.error(new DefaultBundleKey("identity_loginFailed")).defaults("Invalid username or password");        setStatus(AuthenticationStatus.FAILURE);            }}

热点排行