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