Browse Source

三方登录,

root 5 years ago
parent
commit
a956fc77af

+ 32 - 39
facade/src/app/routes/passport/login/login.component.ts

@@ -97,13 +97,28 @@ export class UserLoginComponent implements OnDestroy {
97 97
   // #endregion
98 98
 
99 99
   submit() {
100
+    this.userName.markAsDirty();
101
+    this.userName.updateValueAndValidity();
102
+    this.password.markAsDirty();
103
+    this.password.updateValueAndValidity();
104
+    if (this.userName.invalid || this.password.invalid) return;
105
+    const info = {
106
+      username: this.userName.value,
107
+      password: this.password.value,
108
+    };
109
+
110
+    this.login(info);
111
+  }
112
+
113
+
114
+  /**
115
+   * 执行登录
116
+   * @param params 登录参数
117
+   */
118
+  login(info: object) {
100 119
     this.error = '';
101 120
     // if (this.type === 0) {
102
-      this.userName.markAsDirty();
103
-      this.userName.updateValueAndValidity();
104
-      this.password.markAsDirty();
105
-      this.password.updateValueAndValidity();
106
-      if (this.userName.invalid || this.password.invalid) return;
121
+
107 122
     // } else {
108 123
     //   this.mobile.markAsDirty();
109 124
     //   this.mobile.updateValueAndValidity();
@@ -120,9 +135,9 @@ export class UserLoginComponent implements OnDestroy {
120 135
         client_id: this.settingsService.user.client.clientId || environment.client_id,
121 136
         client_secret: this.settingsService.user.client.clientSecret || environment.client_secret,
122 137
         scope: '*',
123
-        username: this.userName.value,
124
-        password: this.password.value,
125
-        provider: environment.provider,
138
+        username: info['username'],
139
+        password: info['password'],
140
+        type: info['type'] || 0,
126 141
       };
127 142
 
128 143
 
@@ -164,6 +179,7 @@ export class UserLoginComponent implements OnDestroy {
164 179
       });
165 180
   }
166 181
 
182
+
167 183
   // #region social
168 184
 
169 185
   open(item: object, openType: SocialOpenType = 'href') {
@@ -217,7 +233,8 @@ export class UserLoginComponent implements OnDestroy {
217 233
           type: 'window',
218 234
         })
219 235
         .subscribe(res => {
220
-          const code = localStorage.getItem('_code');
236
+          const code = localStorage.getItem('_token_');
237
+          this.loginByToken(item, code);
221 238
           //this.loadUser(item, code);
222 239
           // if (res) {
223 240
           //   this.settingsService.setUser(res);
@@ -231,37 +248,13 @@ export class UserLoginComponent implements OnDestroy {
231 248
     }
232 249
   }
233 250
 
234
-  loadUser(item: object, code: string) {
235
-    // 获得token
236
-    const data = {
237
-      grant_type: 'authorization_code',
238
-      client_id: item['clientId'],
239
-      client_secret: item['clientSecret'],
240
-      code: code,
241
-      redirect_uri: '',
251
+  loginByToken(item: object, token: string) {
252
+    const info = {
253
+      username: item['code'],
254
+      password: token,
255
+      type: 9,
242 256
     };
243
-
244
-
245
-  const url = environment.BASE_URL + 'oauth/token?_allow_anonymous=true';
246
-  let options = null;
247
-  let params: any = null;
248
-  let body = null;
249
-  if ( environment.auth_set_header ) {
250
-     options = {headers:   {Authorization: 'Basic ' + btoa ( data.client_id + ':' + data.client_secret ),
251
-       'Content-type': 'application/x-www-form-urlencoded'}};
252
-    params = data;
253
-  } else {
254
-    body = data;
255
-  }
256
-console.log(item);
257
-
258
-  this.http
259
-    .post('http://127.0.0.1:8092/oauth/token' , body , data, options)
260
-    .subscribe((res: any) => {
261
-      console.log(res);
262
-    } ,  (res) => {
263
-       console.log(res);
264
-    });
257
+    this.login(info);
265 258
   }
266 259
 
267 260
   // #endregion

+ 2 - 2
src/main/java/com/kingkong/bljs/controller/MainController.java

@@ -75,7 +75,7 @@ public class MainController {
75 75
         //threeLoginService.setThreelogin(threelogin);
76 76
 
77 77
 
78
-        //String str = "<script>localStorage.setItem('_code','" + token + "');window.close();</script>";
79
-        return code;
78
+        String str = "<script>localStorage.setItem('_token_','" + token + "');window.close();</script>";
79
+        return str;
80 80
     }
81 81
 }

+ 8 - 1
src/main/java/com/kingkong/bljs/security/CustomClientDetailService.java

@@ -10,6 +10,7 @@ import org.springframework.security.oauth2.provider.ClientDetailsService;
10 10
 import org.springframework.security.oauth2.provider.ClientRegistrationException;
11 11
 import org.springframework.security.oauth2.provider.client.BaseClientDetails;
12 12
 
13
+import javax.servlet.http.HttpSession;
13 14
 import java.util.Arrays;
14 15
 import java.util.HashSet;
15 16
 import java.util.Set;
@@ -19,6 +20,9 @@ public class CustomClientDetailService implements ClientDetailsService {
19 20
     @Autowired
20 21
     private ClientMapper clientMapper;
21 22
 
23
+    @Autowired
24
+    private HttpSession session;
25
+
22 26
     @Override
23 27
     public ClientDetails loadClientByClientId(String s) throws ClientRegistrationException {
24 28
 
@@ -35,7 +39,10 @@ public class CustomClientDetailService implements ClientDetailsService {
35 39
         Set<String> uris = new HashSet<>();
36 40
         uris.add(client.getRedirectUri());
37 41
         clientDetails.setRegisteredRedirectUri(uris);
38
-        clientDetails.setAccessTokenValiditySeconds(3600*24);
42
+        clientDetails.setAccessTokenValiditySeconds(3600*24*7);
43
+
44
+        session.setAttribute("_app_id",client.getAppId());
45
+
39 46
         return clientDetails;
40 47
     }
41 48
 }

+ 27 - 7
src/main/java/com/kingkong/bljs/security/CustomUserDetailsService.java

@@ -34,6 +34,9 @@ public class CustomUserDetailsService implements UserDetailsService {
34 34
     @Autowired
35 35
     private HttpServletRequest request;
36 36
 
37
+    @Autowired
38
+    private HttpSession session;
39
+
37 40
 
38 41
 
39 42
     @Value("${app.type}")
@@ -44,15 +47,32 @@ public class CustomUserDetailsService implements UserDetailsService {
44 47
 
45 48
     @Override
46 49
     public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
50
+        String appid = this.request.getParameter("app_id");
51
+        if(null == appid){
52
+            Object tmp = session.getAttribute("_app_id");
53
+            if(null == tmp)
54
+                appid = "0";
55
+            else
56
+                appid = tmp.toString();
57
+        }
47 58
 
48 59
         if(1== appType)
49
-            return this.zlUser(s);
60
+            return this.zlUser(s,appid);
61
+
62
+        String type =  this.request.getParameter("type");
63
+        Map user = null;
64
+        if(type.equals("9")){   //第三方登录
65
+            String userType = request.getParameter("username");
66
+            String token = request.getParameter("password");
67
+            user = queryService.find("select * from k_user where type='{0}' and password='{1}'",userType,token);
68
+        }else{
69
+            user = queryService.find("select * from k_user where name='{0}' and type is null",s);
70
+        }
50 71
 
51
-        Map user = queryService.first("k_user","name=" + s);
52 72
         if(null == user )
53 73
             throw new UsernameNotFoundException("用户不存在!");
54 74
 
55
-        String appid = this.request.getParameter("app_id");
75
+
56 76
         Map condition = new HashMap();
57 77
         condition.put("u_id",user.get("id").toString());
58 78
         condition.put("app_id",appid);
@@ -69,18 +89,18 @@ public class CustomUserDetailsService implements UserDetailsService {
69 89
         return customUser;
70 90
     }
71 91
 
72
-    private CustomUser zlUser(String userName) throws UsernameNotFoundException{
73
-        String strSQL =  "select user_id id,username name,DECODE(t.username,'ZLHIS',1,0) is_super,100 app_id\n" +
92
+    private CustomUser zlUser(String userName,String appid) throws UsernameNotFoundException{
93
+        String strSQL =  "select user_id id,username name,DECODE(t.username,'ZLHIS',1,0) is_super,{1} app_id\n" +
74 94
                         "from all_users t,上机人员表 z\n" +
75 95
                         "where t.username='{0}' and t.username = z.用户名";
76 96
 
77
-        Map user = this.queryService.find(strSQL,userName);
97
+        Map user = this.queryService.find(strSQL,userName,appid);
78 98
         if(null == user)
79 99
             throw new UsernameNotFoundException("用户不存在!");
80 100
         CustomUser customUser =   new CustomUser();
81 101
 
82 102
         customUser.setUser(user);
83
-        customUser.setAppid("100");
103
+        customUser.setAppid(appid);
84 104
         return customUser;
85 105
     }
86 106
 }

+ 15 - 3
src/main/java/com/kingkong/bljs/security/CustomtAuthenticationProvider.java

@@ -10,6 +10,8 @@ import org.springframework.security.core.AuthenticationException;
10 10
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
11 11
 import org.springframework.stereotype.Component;
12 12
 
13
+import javax.servlet.http.HttpServletRequest;
14
+
13 15
 /**
14 16
  * 自定义登录
15 17
  */
@@ -28,6 +30,9 @@ public class CustomtAuthenticationProvider implements AuthenticationProvider  {
28 30
     @Value("${app.server}")
29 31
     private String server;
30 32
 
33
+    @Autowired
34
+    private HttpServletRequest request;
35
+
31 36
 
32 37
     @Override
33 38
     public Authentication authenticate(Authentication authentication) throws AuthenticationException {
@@ -42,9 +47,16 @@ public class CustomtAuthenticationProvider implements AuthenticationProvider  {
42 47
                 throw new BadCredentialsException("密码不正确!");
43 48
         } else {
44 49
             user = (CustomUser)userDetailsService.loadUserByUsername(username);
45
-            BCryptPasswordEncoder encoder  = new BCryptPasswordEncoder();
46
-            if(! encoder.matches(password,user.getPassword()))
47
-                throw new BadCredentialsException("密码不正确!");
50
+            String loginType =  this.request.getParameter("type");
51
+            if(loginType.equals("9")){ //三方登录不需要验证
52
+
53
+            }
54
+            else{
55
+                BCryptPasswordEncoder encoder  = new BCryptPasswordEncoder();
56
+                if(! encoder.matches(password,user.getPassword()))
57
+                    throw new BadCredentialsException("密码不正确!");
58
+            }
59
+
48 60
         }
49 61
 
50 62
 

+ 30 - 1
src/main/java/com/kingkong/bljs/service/ThreeLoginService.java

@@ -12,6 +12,7 @@ import org.springframework.util.MultiValueMap;
12 12
 
13 13
 import javax.servlet.http.HttpServletRequest;
14 14
 import java.util.HashMap;
15
+import java.util.Map;
15 16
 
16 17
 
17 18
 @Service
@@ -37,7 +38,7 @@ public class ThreeLoginService {
37 38
 
38 39
 
39 40
     public Threelogin getThreelogin(){
40
-        return this.getThreelogin();
41
+        return this.threelogin;
41 42
     }
42 43
 
43 44
     public String getToken() {
@@ -88,6 +89,21 @@ public class ThreeLoginService {
88 89
         return JSONObject.parse(res);
89 90
     }
90 91
 
92
+
93
+    public Object postData(String api,Map map) throws Exception {
94
+        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
95
+        if(null != map) {
96
+            for(Object o : map.keySet()) {
97
+                params.add(o.toString(),null == map.get(o) ? "": map.get(o).toString());
98
+            }
99
+        }
100
+        return post(api,params);
101
+    }
102
+
103
+    public Object postData(String api) throws Exception {
104
+        return postData(api,null);
105
+    }
106
+
91 107
     public Object get(String api,MultiValueMap<String, String> params) throws Exception{
92 108
         HttpHeaders httpHeaders = this.getHeader();
93 109
 
@@ -98,4 +114,17 @@ public class ThreeLoginService {
98 114
         return JSONObject.parse(res);
99 115
     }
100 116
 
117
+    public Object getData(String api,Map map) throws Exception {
118
+        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
119
+        if(null != map) {
120
+            for(Object o : map.keySet()) {
121
+                params.add(o.toString(),null == map.get(o) ? "": map.get(o).toString());
122
+            }
123
+        }
124
+        return get(api,params);
125
+    }
126
+
127
+    public Object getData(String api) throws Exception{
128
+        return getData(api,null);
129
+    }
101 130
 }

+ 14 - 15
src/main/java/com/kingkong/bljs/service/UserService.java

@@ -31,11 +31,11 @@ public class UserService {
31 31
     private HttpServletRequest request;
32 32
 
33 33
 
34
-    @Value("${app.table.user}")
35
-    private String k_user;
36
-
37
-    @Value("${app.table.app_user}")
38
-    private String k_app_user;
34
+//    @Value("${app.table.user}")
35
+//    private String k_user;
36
+//
37
+//    @Value("${app.table.app_user}")
38
+//    private String k_app_user;
39 39
 
40 40
     @Value("${app.type}")
41 41
     private String appType;
@@ -82,14 +82,13 @@ public class UserService {
82 82
         String strSQL = "";
83 83
         if(appType.equals("1")) {   //中联账号体系
84 84
 
85
-            strSQL = "select  t.id,t.name,null email,null avatar,t.is_super ,a.id staff_id,a.name staff,a.code staff_code,a.note,z.s_id,\n" +
86
-                    "	case when a.gender = 1 then '男' else '女' end gender_name,null tel,c.name department\n" +
87
-                    "from " + k_user + " t\n" +
88
-                    "LEFT JOIN k_user_staff z on z.u_id = t.id and z.app_id = {1}\n" +
89
-                    "LEFT JOIN k_staff a on a.app_id = z.app_id and a.id = z.s_id\n" +
90
-                    "LEFT JOIN k_staff_department b on b.s_id = a.id\n" +
91
-                    "LEFT JOIN k_department c on c.app_id = z.app_id and c.id = b.d_id\n" +
92
-                    "where t.name='{0}'";
85
+            strSQL =  "select a.user_id id,t.用户名 name,null email,null avatar,decode(t.用户名,'ZLHIS',1,0) is_super,\n" +
86
+                            "       z.id staff_id,z.姓名 staff,z.编号 staff_code,z.个人简介 note,t.人员id s_id,\n" +
87
+                            "       z.性别 gender_name,z.移动电话 tel,null department,-1 user_type\n" +
88
+                            "from all_users a,上机人员表 t ,人员表 z\n" +
89
+                            "where a.username=t.用户名 and z.id(+) = t.人员id\n" +
90
+                            "      and a.username='{0}'";
91
+
93 92
 
94 93
 
95 94
             user = queryService.find(strSQL, name, appid);
@@ -106,9 +105,9 @@ public class UserService {
106 105
                 user.put("departments", new ArrayList<Map>());
107 106
             }
108 107
         }else {
109
-            strSQL = "select  t.id,t.name,null email,null avatar,t.is_super ,null staff_id,null staff,null staff_code, null note,null s_id,\n" +
108
+            strSQL = "select  t.type user_type,t.id,t.name,null email,null avatar,t.is_super ,null staff_id,null staff,null staff_code, null note,null s_id,\n" +
110 109
                     "	null gender_name,null tel,null department\n" +
111
-                    "from " + k_user + " t\n" +
110
+                    "from k_user t\n" +
112 111
                     "where t.name='{0}'";
113 112
 
114 113
             user = queryService.find(strSQL, name, appid);