`
lxcaoxin
  • 浏览: 125131 次
  • 性别: Icon_minigender_1
  • 来自: 南宁
社区版块
存档分类
最新评论

seam3 登陆验证

    博客分类:
  • Seam
阅读更多
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 class="errors"><h:message for="username"/></div>
            </div>
            <div>
               <h:outputLabel for="password" value="#{bundles.messages.home_passwordLabel}"/>
              [color=red]<h:inputSecret id="password" value="#{credentials.password}" style="width: 175px;"/>[/color] 
            </div>
            <span class="errors">
               <h:messages errorClass="error" styleClass="messages" id="messages" globalOnly="true"/>
            </span>
            <div class="buttonBox">[color=red]<h:commandButton id="login" action="#{identity.login}" value="#{bundles.messages.home_loginAction}"/>[/color]</div>
            <div class="notes"><h:link id="register" outcome="/register.xhtml" value="#{bundles.messages.home_registerAction}"/></div>
            <div class="subnotes">
               #{bundles.messages.home_useDemoAccount}
               <ul>
                  <li>shane/brisbane</li>
                  <li>dan/laurel</li>
                  <li>lincoln/charlotte</li>
                  <li>jose/brazil</li>
               </ul>
            </div>
         </fieldset>
      </h:form>


3.配置 验证方法(classpath下的resources文件夹seam-beans.xml)
<?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);
        
    }

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics