Mark 7 years ago
parent
commit
3fed70f053

+ 9 - 7
src/main/java/io/renren/modules/sys/controller/SysLoginController.java

@ -5,11 +5,13 @@ import com.google.code.kaptcha.Producer;
5 5
import io.renren.common.utils.R;
6 6
import io.renren.common.utils.ShiroUtils;
7 7
import io.renren.modules.sys.entity.SysUserEntity;
8
import io.renren.modules.sys.form.LoginForm;
8 9
import io.renren.modules.sys.service.SysUserService;
9 10
import io.renren.modules.sys.service.SysUserTokenService;
10 11
import org.apache.commons.io.IOUtils;
11 12
import org.apache.shiro.crypto.hash.Sha256Hash;
12 13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.web.bind.annotation.RequestBody;
13 15
import org.springframework.web.bind.annotation.RequestMapping;
14 16
import org.springframework.web.bind.annotation.RequestMethod;
15 17
import org.springframework.web.bind.annotation.RestController;
@ -62,19 +64,19 @@ public class SysLoginController extends AbstractController {
62 64
	 * 登录
63 65
	 */
64 66
	@RequestMapping(value = "/sys/login", method = RequestMethod.POST)
65
	public Map<String, Object> login(String username, String password, String captcha)throws IOException {
67
	public Map<String, Object> login(@RequestBody LoginForm form)throws IOException {
66 68
		//本项目已实现,前后端完全分离,但页面还是跟项目放在一起了,所以还是会依赖session
67 69
		//如果想把页面单独放到nginx里,实现前后端完全分离,则需要把验证码注释掉(因为不再依赖session了)
68
		String kaptcha = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
69
		if(!captcha.equalsIgnoreCase(kaptcha)){
70
			return R.error("验证码不正确");
71
		}
70
//		String kaptcha = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
71
//		if(!captcha.equalsIgnoreCase(kaptcha)){
72
//			return R.error("验证码不正确");
73
//		}
72 74
73 75
		//用户信息
74
		SysUserEntity user = sysUserService.queryByUserName(username);
76
		SysUserEntity user = sysUserService.queryByUserName(form.getUsername());
75 77
76 78
		//账号不存在、密码错误
77
		if(user == null || !user.getPassword().equals(new Sha256Hash(password, user.getSalt()).toHex())) {
79
		if(user == null || !user.getPassword().equals(new Sha256Hash(form.getPassword(), user.getSalt()).toHex())) {
78 80
			return R.error("账号或密码不正确");
79 81
		}
80 82

+ 5 - 4
src/main/java/io/renren/modules/sys/controller/SysUserController.java

@ -10,6 +10,7 @@ import io.renren.common.validator.ValidatorUtils;
10 10
import io.renren.common.validator.group.AddGroup;
11 11
import io.renren.common.validator.group.UpdateGroup;
12 12
import io.renren.modules.sys.entity.SysUserEntity;
13
import io.renren.modules.sys.form.PasswordForm;
13 14
import io.renren.modules.sys.service.SysUserRoleService;
14 15
import io.renren.modules.sys.service.SysUserService;
15 16
import org.apache.commons.lang.ArrayUtils;
@ -70,13 +71,13 @@ public class SysUserController extends AbstractController {
70 71
	 */
71 72
	@SysLog("修改密码")
72 73
	@RequestMapping("/password")
73
	public R password(String password, String newPassword){
74
		Assert.isBlank(newPassword, "新密码不为能空");
74
	public R password(@RequestBody PasswordForm form){
75
		Assert.isBlank(form.getNewPassword(), "新密码不为能空");
75 76
		
76 77
		//sha256加密
77
		password = new Sha256Hash(password, getUser().getSalt()).toHex();
78
		String password = new Sha256Hash(form.getPassword(), getUser().getSalt()).toHex();
78 79
		//sha256加密
79
		newPassword = new Sha256Hash(newPassword, getUser().getSalt()).toHex();
80
		String newPassword = new Sha256Hash(form.getNewPassword(), getUser().getSalt()).toHex();
80 81
				
81 82
		//更新密码
82 83
		int count = sysUserService.updatePassword(getUserId(), password, newPassword);

+ 53 - 0
src/main/java/io/renren/modules/sys/form/LoginForm.java

@ -0,0 +1,53 @@
1
/**
2
 * Copyright 2018 人人开源 http://www.renren.io
3
 * <p>
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
 * use this file except in compliance with the License. You may obtain a copy of
6
 * the License at
7
 * <p>
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 * <p>
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 * License for the specific language governing permissions and limitations under
14
 * the License.
15
 */
16
17
package io.renren.modules.sys.form;
18
19
/**
20
 * 登录表单
21
 *
22
 * @author Mark sunlightcs@gmail.com
23
 * @since 1.4.0 2018-01-25
24
 */
25
public class LoginForm {
26
    private String username;
27
    private String password;
28
    private String captcha;
29
30
    public String getUsername() {
31
        return username;
32
    }
33
34
    public void setUsername(String username) {
35
        this.username = username;
36
    }
37
38
    public String getPassword() {
39
        return password;
40
    }
41
42
    public void setPassword(String password) {
43
        this.password = password;
44
    }
45
46
    public String getCaptcha() {
47
        return captcha;
48
    }
49
50
    public void setCaptcha(String captcha) {
51
        this.captcha = captcha;
52
    }
53
}

+ 50 - 0
src/main/java/io/renren/modules/sys/form/PasswordForm.java

@ -0,0 +1,50 @@
1
/**
2
 * Copyright 2018 人人开源 http://www.renren.io
3
 * <p>
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
 * use this file except in compliance with the License. You may obtain a copy of
6
 * the License at
7
 * <p>
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 * <p>
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 * License for the specific language governing permissions and limitations under
14
 * the License.
15
 */
16
17
package io.renren.modules.sys.form;
18
19
/**
20
 * 密码表单
21
 *
22
 * @author Mark sunlightcs@gmail.com
23
 * @since 1.4.0 2018-01-25
24
 */
25
public class PasswordForm {
26
    /**
27
     * 原密码
28
     */
29
    private String password;
30
    /**
31
     * 新密码
32
     */
33
    private String newPassword;
34
35
    public String getPassword() {
36
        return password;
37
    }
38
39
    public void setPassword(String password) {
40
        this.password = password;
41
    }
42
43
    public String getNewPassword() {
44
        return newPassword;
45
    }
46
47
    public void setNewPassword(String newPassword) {
48
        this.newPassword = newPassword;
49
    }
50
}

+ 4 - 4
src/main/java/io/renren/modules/sys/service/impl/SysUserRoleServiceImpl.java

@ -26,12 +26,12 @@ public class SysUserRoleServiceImpl implements SysUserRoleService {
26 26
27 27
	@Override
28 28
	public void saveOrUpdate(Long userId, List<Long> roleIdList) {
29
		if(roleIdList.size() == 0){
30
			return ;
31
		}
32
		
33 29
		//先删除用户与角色关系
34 30
		sysUserRoleDao.delete(userId);
31
32
		if(roleIdList == null || roleIdList.size() == 0){
33
			return ;
34
		}
35 35
		
36 36
		//保存用户与角色关系
37 37
		Map<String, Object> map = new HashMap<>();

+ 6 - 4
src/main/resources/static/js/index.js

@ -38,8 +38,10 @@ var vm = new Vue({
38 38
		user:{},
39 39
		menuList:{},
40 40
		main:"main.html",
41
		password:'',
42
		newPassword:'',
41
        form:{
42
            password:'',
43
            newPassword:''
44
		},
43 45
        navTitle:"欢迎页"
44 46
	},
45 47
	methods: {
@ -64,12 +66,12 @@ var vm = new Vue({
64 66
				content: jQuery("#passwordLayer"),
65 67
				btn: ['修改','取消'],
66 68
				btn1: function (index) {
67
					var data = "password="+vm.password+"&newPassword="+vm.newPassword;
68 69
					$.ajax({
69 70
						type: "POST",
70 71
					    url: baseURL + "sys/user/password",
71
					    data: data,
72 72
					    dataType: "json",
73
                        contentType: "application/json",
74
                        data: JSON.stringify(vm.form),
73 75
					    success: function(r){
74 76
							if(r.code == 0){
75 77
								layer.close(index);

+ 31 - 22
src/main/resources/static/swagger/index.yaml

@ -33,20 +33,12 @@ paths:
33 33
      produces:
34 34
        - application/json
35 35
      parameters:
36
        - name: username
37
          description: 用户名
38
          in: query
39
          type: string
40
          required: true
41
        - name: password
42
          description: 密码
43
          in: query
44
          type: string
45
          required: true
46
        - name: captcha
47
          description: 验证码
48
          in: query
36
        - name: body
37
          description: 管理员对象
38
          in: body
49 39
          type: string
40
          schema:
41
            $ref: '#/definitions/LoginForm'
50 42
          required: true
51 43
      responses:
52 44
        '200':
@ -140,15 +132,12 @@ paths:
140 132
      produces:
141 133
        - application/json
142 134
      parameters:
143
        - name: password
144
          description: 原密码
145
          in: query
146
          type: string
147
          required: true
148
        - name: newPassword
149
          description: 新密码
150
          in: query
135
        - name: body
136
          description: 管理员对象
137
          in: body
151 138
          type: string
139
          schema:
140
            $ref: '#/definitions/PasswordForm'
152 141
          required: true
153 142
      responses:
154 143
        '200':
@ -1050,7 +1039,27 @@ definitions:
1050 1039
        msg:
1051 1040
          description: 失败原因
1052 1041
          type: string
1053
1042
  LoginForm:
1043
    type: object
1044
    properties:
1045
      username:
1046
        description: 用户名
1047
        type: string
1048
      password:
1049
        description: 密码
1050
        type: string
1051
      captcha:
1052
        description: 验证码
1053
        type: string
1054
  PasswordForm:
1055
    type: object
1056
    properties:
1057
      password:
1058
        description: 原密码
1059
        type: string
1060
      newPassword:
1061
        description: 新密码
1062
        type: string
1054 1063
  SysUserEntity:
1055 1064
    type: object
1056 1065
    properties:

+ 2 - 2
src/main/resources/views/index.html

@ -108,13 +108,13 @@
108 108
		<div class="form-group">
109 109
		   	<div class="col-sm-2 control-label">原密码</div>
110 110
		   	<div class="col-sm-10">
111
		      <input type="password" class="form-control" v-model="password" placeholder="原密码"/>
111
		      <input type="password" class="form-control" v-model="form.password" placeholder="原密码"/>
112 112
		    </div>
113 113
		</div>
114 114
		<div class="form-group">
115 115
		   	<div class="col-sm-2 control-label">新密码</div>
116 116
		   	<div class="col-sm-10">
117
		      <input type="text" class="form-control" v-model="newPassword" placeholder="新密码"/>
117
		      <input type="text" class="form-control" v-model="form.newPassword" placeholder="新密码"/>
118 118
		    </div>
119 119
		</div>
120 120
	</div>

+ 10 - 8
src/main/resources/views/login.html

@ -33,15 +33,15 @@
33 33
        <h4 style="margin-bottom: 0px;"><i class="fa fa-exclamation-circle"></i> {{errorMsg}}</h4>
34 34
      </div>
35 35
      <div class="form-group has-feedback">
36
          <input type="text" class="form-control" v-model="username" placeholder="账号">
36
          <input type="text" class="form-control" v-model="form.username" placeholder="账号">
37 37
          <span class="glyphicon glyphicon-user form-control-feedback"></span>
38 38
      </div>
39 39
      <div class="form-group has-feedback">
40
          <input type="password" class="form-control" v-model="password" placeholder="密码">
40
          <input type="password" class="form-control" v-model="form.password" placeholder="密码">
41 41
          <span class="glyphicon glyphicon-lock form-control-feedback"></span>
42 42
      </div>
43 43
      <div class="form-group has-feedback">
44
          <input type="text" class="form-control" v-model="captcha" @keyup.enter="login" placeholder="验证码">
44
          <input type="text" class="form-control" v-model="form.captcha" @keyup.enter="login" placeholder="验证码">
45 45
          <span class="glyphicon glyphicon-warning-sign form-control-feedback"></span>
46 46
      </div>
47 47
      <div class="form-group has-feedback">
@ -76,9 +76,11 @@
76 76
    var vm = new Vue({
77 77
        el:'#rrapp',
78 78
        data:{
79
            username: '',
80
            password: '',
81
            captcha: '',
79
            form: {
80
                username: '',
81
                password: '',
82
                captcha: ''
83
            },
82 84
            error: false,
83 85
            errorMsg: '',
84 86
            src: 'captcha.jpg'
@ -93,12 +95,12 @@
93 95
                this.src = "captcha.jpg?t=" + $.now();
94 96
            },
95 97
            login: function () {
96
                var data = "username="+vm.username+"&password="+vm.password+"&captcha="+vm.captcha;
97 98
                $.ajax({
98 99
                    type: "POST",
99 100
                    url: baseURL + "sys/login",
100
                    data: data,
101 101
                    dataType: "json",
102
                    contentType: "application/json",
103
                    data: JSON.stringify(vm.form),
102 104
                    success: function(r){
103 105
                        if(r.code == 0){//登录成功
104 106
                            localStorage.setItem("token", r.token);