seam3 登陆验证

[b]seam3 登陆验证(Seam3 Booking example)[/b]
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);

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值