signupcontroller.java
来自「Java的框架」· Java 代码 · 共 146 行
JAVA
146 行
package mcaps.core.user.webapp.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.providers.ProviderManager;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.BindException;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import mcap.core.base.webapp.controller.BaseFormController;
import mcap.core.base.webapp.util.RequestUtil;
import mcap.core.config.Config;
import mcap.core.user.model.PasswordControl;
import mcap.core.user.model.User;
import mcap.core.user.service.PasswordControlManager;
import mcap.core.user.service.RoleManager;
import mcap.core.user.service.UserExistsException;
import mcap.core.user.util.NameConstants;
import mcap.core.util.PasswordUtil;
/**
* Taken from AppFuse. Controller to signup new users.
*/
public class SignupController extends BaseFormController {
private RoleManager roleManager;
private PasswordControlManager passwordControlManager;
/**
* Returns the passwordControlManager.
* @return PasswordControlManager
*/
public PasswordControlManager getPasswordControlManager () {
return passwordControlManager;
}
/**
* Sets the passwordControlManager.
* @param passwordControlManager The passwordControlManager to set.
*/
public void setPasswordControlManager (
PasswordControlManager passwordControlManager) {
this.passwordControlManager = passwordControlManager;
}
/**
* @param roleManager The roleManager to set.
*/
public void setRoleManager (RoleManager roleManager) {
this.roleManager = roleManager;
}
public ModelAndView onSubmit (HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
User user = (User) command;
Locale locale = request.getLocale ();
Config config = Config.getInstance ();
String algorithm = config.getValue (NameConstants.ENC_ALGORITHM);
Integer keySize = new Integer (config.getValue (NameConstants.ENC_KEY_SIZE));
if (algorithm == null) { // should only happen for test case
algorithm = "SHA";
}
user.setPassword (PasswordUtil.encodePassword (user.getPassword (), user
.getUsername (), algorithm, keySize));
user.setEnabled (true);
user.setAccountLocked (false);
// Set the default user role on this new user
user.addRole (roleManager.getRole (NameConstants.USER_ROLE));
try {
this.getUserManager ().saveUser (user);
}
catch (UserExistsException e) {
errors.rejectValue ("username", "errors.existing.user", new Object[] {
user.getUsername (), user.getEmail () }, "duplicate user");
// redisplay the unencrypted passwords
user.setPassword (user.getConfirmPassword ());
return showForm (request, response, errors);
}
saveMessage (request, getText ("user.registered", user.getUsername (),
locale));
if (this.passwordControlManager != null) {
PasswordControl control = new PasswordControl ();
control.setUsername (user.getUsername ());
control.setLastModifiedDate (new Date ());
control.setNeedPasswordChange (false);
List list = new ArrayList ();
list.add (user.getPassword ());
control.setPasswordHistory (list);
this.passwordControlManager.savePasswordControl (control);
}
// log user in automatically
Authentication auth = new UsernamePasswordAuthenticationToken (user
.getUsername (), user.getConfirmPassword ());
try {
ApplicationContext ctx = WebApplicationContextUtils
.getWebApplicationContext (request.getSession ().getServletContext ());
if (ctx != null) {
ProviderManager authenticationManager = (ProviderManager) ctx
.getBean ("authenticationManager");
SecurityContextHolder.getContext ().setAuthentication (
authenticationManager.doAuthentication (auth));
}
}
catch (NoSuchBeanDefinitionException n) {
// ignore, should only happen when testing
}
// Send an account information e-mail
message.setSubject (getText ("signup.email.subject", locale));
sendUserMessage (user, getText ("signup.email.message", locale),
RequestUtil.getAppURL (request));
return new ModelAndView (getSuccessView ());
}
protected Object formBackingObject (HttpServletRequest request)
throws Exception {
return new User ();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?