1.前端JSP页面。 webapp——common——taglibs.jsp
Ext.onReady(function(){
if(undefined!=Ext.get(' navigationButtonTag ')){
Ext.Function.defer(function() {
new Ext.Button({
xtype: ' button ',
iconCls: ' logout-icon ',
cls: ' pointer_btn ',
border:0,
scale: ' small ',
press: false,
renderTo: ' navigationButtonTag ',
menu: [
{
text:' 修改密码 ',
hander:function(){
Ext.widget(" passwordChange ");
}
},
{
text:' 登出 ',
hander: function(){
location.href = ctx+' /logout '
}
}
]
});
},1000);
}
});
2.yucen——app——view——password——PasswordChange.js
Ext.define("yucen.view.password.PasswordChange", {
extend: "Ext.form.Panel",
alias: "widget.passwordChange",
url:ctx+"/so/changeUserPwd",
require: [],
autoScroll: true,
defaults: {
xtype: 'textfield',
width: 300
},
layout: {
type: 'vbox',
align: 'center',
padding: 80
},
initComponent: function () {
this.items = [
{fieldLabel: '原始密码'+'*', id: 'oldPassword', name: 'oldPwd', inputType: "password",
allowBlank: false,
blankText: '原始密码不能为空!',
minLength: 5,
maxLength: 20,
minLengthText: "请输入6~20位密码!",
maxLengthText: '请输入6~20位密码!'},
{fieldLabel: '新密码'+'*', id: 'password', name: 'newPwd', inputType: "password",
allowBlank: false,
blankText: '新密码不能为空!',
minLength: 6,
maxLength: 20,
minLengthText: "请输入6~20位密码!",
maxLengthText: '请输入6~20位密码!',
validator: function () {
if (this.getValue() == Ext.getCmp('confirmPassword').getValue()) {
Ext.getCmp('confirmPassword').clearInvalid()
}
if (this.getValue() != Ext.getCmp('oldPassword').getValue()) {
return true;
} else if(Ext.getCmp('oldPassword').getValue()!=""){
return "新密码不能与原密码相同";
}
}},
{fieldLabel: '确认密码'+'*', id: 'confirmPassword', name: 'confirmPassword', inputType: "password",
validator: function () {
if (this.getValue() == Ext.getCmp('password').getValue()) {
return true;
} else {
return "两次密码不匹配";
}
},
allowBlank: false,
blankText: '确认密码不能为空!',
minLength: 6,
maxLength: 20,
minLengthText: "请输入6~20位密码!",
maxLengthText: '请输入6~20位密码!'
},{
xtype:'hidden',
name:'username',
value:'admin'
}
];
this.dockedItems = [
{
xtype: 'toolbar',
width: '100%',
items: [
{text: '保存', action: 'changePassword'}
]
}
],
this.callParent(arguments);
}
});
3.创建SystemUserController,并写接口:/changeUserPwd
@RequestMapping(value = "/changeUserPwd", method = RequestMethod.POST)
@ResponseBody
public JsonObjectResponse changeUserPwd(HttpSession session,
@RequestParam(value = "username") String username,
@RequestParam(value = "oldPwd") String oldPwd,
@RequestParam(value = "newPwd") String newPwd,
HttpServletRequest request ) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("--------------changeUserPwd---------------");
logger.debug("phoneNo = " + username);
logger.debug("oldPwd = " + oldPwd);
logger.debug("newPwd = " + newPwd);
}
User user = userManager.getUserByUsername(username);
if (null == user) {
logger.debug("登录已失效,请重新登录");
logger.debug("--------------------------------------");
return JsonObjectResponse.failResponseWithMessageCode("登录已失效,请重新登录", Constants.LOGIN_INVALID);
} else {
logger.debug("当前登录用户id:" + user.getId());
logger.debug("当前登录用户手机号:" + user.getPhoneNumber());
}
if (oldPwd.equals(newPwd)) {
logger.debug("修改密码失败,新密码不能与原密码相同");
logger.debug("--------------------------------------");
return JsonObjectResponse.failResponseWithMessage("修改密码失败,新密码不能与原密码相同");
}
try {
boolean result = userManager.changePassword(user, newPwd, oldPwd);//changePwd for phoneNo oldPwd newPwd
if (result) {
logger.debug("修改密码成功");
logger.debug("--------------------------------------");
return JsonObjectResponse.successResponseWithMessage("修改密码成功");
} else {
logger.debug("修改密码失败,原密码输入错误");
logger.debug("--------------------------------------");
return JsonObjectResponse.failResponseWithMessage("修改密码失败,原密码输入错误");
}
}catch (Exception e) {
e.printStackTrace();
logger.debug("修改密码失败,原密码输入错误");
logger.debug("--------------------------------------");
return JsonObjectResponse.failResponseWithMessage("修改密码失败,原密码输入错误");
}
}
4.两个service方法getUserByUsername和changePassword
@Override
public User getUserByUsername(final String username) throws UsernameNotFoundException {
return (User) userDao.loadUserByUsername(username);
}
@Override
public boolean changePassword(User user, String password,String oldPassword) {
if(passwordEncoder.matches(oldPassword,user.getPassword())){
User savedUser=this.getUser(user.getId()+"");
savedUser.setPassword(passwordEncoder.encode(password));
userDao.save(savedUser);
log.info("系统用户的密码已经修改为"+password);
return true;
}else{
return false;
}
}
5.Dao层 UserDaoHibernate 的函数:
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
List users = getSession().createCriteria(User.class).add(Restrictions.eq("username", username)).list();
if (users == null || users.isEmpty()) {
throw new UsernameNotFoundException("user '" + username + "' not found...");
} else {
return (UserDetails) users.get(0);
}
}