上QQ阅读APP看书,第一时间看更新
Updating CalendarUserAuthenticationProvider
Let's take a look at the following steps for updating the CalendarUserAuthenticationProvider.java file:
- Now, we need to update CalendarUserAuthenticationProvider to utilize the domain field as follows:
//src/main/java/com/packtpub/springsecurity/authentication/
CalendarUserAuthenticationProvider.java
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
DomainUsernamePasswordAuthenticationToken token =
(DomainUsernamePasswordAuthenticationToken) authentication;
String userName = token.getName();
String domain = token.getDomain();
String email = userName + "@" + domain;
... previous validation of the user and password ...
return new DomainUsernamePasswordAuthenticationToken(user,
password, domain, authorities);
}
public boolean supports(Class<?> authentication) {
return DomainUsernamePasswordAuthenticationToken
.class.equals(authentication);
}
- We first update the supports method so that Spring Security will pass DomainUsernamePasswordAuthenticationToken into our authenticate method.
- We then use the domain information to create our email address and authenticate, as we had previously done. Admittedly, this example is contrived. However, the example is able to illustrate how to authenticate with an additional parameter.
- The CalendarUserAuthenticationProvider interface can now use the new domain field. However, there is no way for a user to specify the domain. For this, we must update our login.html file.