Mark 7 years ago
parent
commit
3fed70f053

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

5
import io.renren.common.utils.R;
5
import io.renren.common.utils.R;
6
import io.renren.common.utils.ShiroUtils;
6
import io.renren.common.utils.ShiroUtils;
7
import io.renren.modules.sys.entity.SysUserEntity;
7
import io.renren.modules.sys.entity.SysUserEntity;
8
import io.renren.modules.sys.form.LoginForm;
8
import io.renren.modules.sys.service.SysUserService;
9
import io.renren.modules.sys.service.SysUserService;
9
import io.renren.modules.sys.service.SysUserTokenService;
10
import io.renren.modules.sys.service.SysUserTokenService;
10
import org.apache.commons.io.IOUtils;
11
import org.apache.commons.io.IOUtils;
11
import org.apache.shiro.crypto.hash.Sha256Hash;
12
import org.apache.shiro.crypto.hash.Sha256Hash;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.web.bind.annotation.RequestBody;
13
import org.springframework.web.bind.annotation.RequestMapping;
15
import org.springframework.web.bind.annotation.RequestMapping;
14
import org.springframework.web.bind.annotation.RequestMethod;
16
import org.springframework.web.bind.annotation.RequestMethod;
15
import org.springframework.web.bind.annotation.RestController;
17
import org.springframework.web.bind.annotation.RestController;
62
	 * 登录
64
	 * 登录
63
	 */
65
	 */
64
	@RequestMapping(value = "/sys/login", method = RequestMethod.POST)
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
		//本项目已实现,前后端完全分离,但页面还是跟项目放在一起了,所以还是会依赖session
68
		//本项目已实现,前后端完全分离,但页面还是跟项目放在一起了,所以还是会依赖session
67
		//如果想把页面单独放到nginx里,实现前后端完全分离,则需要把验证码注释掉(因为不再依赖session了)
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
			return R.error("账号或密码不正确");
80
			return R.error("账号或密码不正确");
79
		}
81
		}
80
82

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

10
import io.renren.common.validator.group.AddGroup;
10
import io.renren.common.validator.group.AddGroup;
11
import io.renren.common.validator.group.UpdateGroup;
11
import io.renren.common.validator.group.UpdateGroup;
12
import io.renren.modules.sys.entity.SysUserEntity;
12
import io.renren.modules.sys.entity.SysUserEntity;
13
import io.renren.modules.sys.form.PasswordForm;
13
import io.renren.modules.sys.service.SysUserRoleService;
14
import io.renren.modules.sys.service.SysUserRoleService;
14
import io.renren.modules.sys.service.SysUserService;
15
import io.renren.modules.sys.service.SysUserService;
15
import org.apache.commons.lang.ArrayUtils;
16
import org.apache.commons.lang.ArrayUtils;
70
	 */
71
	 */
71
	@SysLog("修改密码")
72
	@SysLog("修改密码")
72
	@RequestMapping("/password")
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
		//sha256加密
77
		//sha256加密
77
		password = new Sha256Hash(password, getUser().getSalt()).toHex();
78
		String password = new Sha256Hash(form.getPassword(), getUser().getSalt()).toHex();
78
		//sha256加密
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
		int count = sysUserService.updatePassword(getUserId(), password, newPassword);
83
		int count = sysUserService.updatePassword(getUserId(), password, newPassword);

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

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

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

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

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

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

33
      produces:
33
      produces:
34
        - application/json
34
        - application/json
35
      parameters:
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
          type: string
39
          type: string
40
          schema:
41
            $ref: '#/definitions/LoginForm'
50
          required: true
42
          required: true
51
      responses:
43
      responses:
52
        '200':
44
        '200':
140
      produces:
132
      produces:
141
        - application/json
133
        - application/json
142
      parameters:
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
          type: string
138
          type: string
139
          schema:
140
            $ref: '#/definitions/PasswordForm'
152
          required: true
141
          required: true
153
      responses:
142
      responses:
154
        '200':
143
        '200':
1050
        msg:
1039
        msg:
1051
          description: 失败原因
1040
          description: 失败原因
1052
          type: string
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
  SysUserEntity:
1063
  SysUserEntity:
1055
    type: object
1064
    type: object
1056
    properties:
1065
    properties:

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

108
		<div class="form-group">
108
		<div class="form-group">
109
		   	<div class="col-sm-2 control-label">原密码</div>
109
		   	<div class="col-sm-2 control-label">原密码</div>
110
		   	<div class="col-sm-10">
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
		    </div>
112
		    </div>
113
		</div>
113
		</div>
114
		<div class="form-group">
114
		<div class="form-group">
115
		   	<div class="col-sm-2 control-label">新密码</div>
115
		   	<div class="col-sm-2 control-label">新密码</div>
116
		   	<div class="col-sm-10">
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
		    </div>
118
		    </div>
119
		</div>
119
		</div>
120
	</div>
120
	</div>

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

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