Browse Source

小程序登录使用配置文件中的appid和appsecret

hym 4 years ago
parent
commit
678d2597fd

+ 3 - 2
src/main/java/com/zoniot/ccrc/security/openid/OpenidLoginAuthenticationFilter.java

@@ -2,6 +2,7 @@ package com.zoniot.ccrc.security.openid;
 
 
 import com.zoniot.ccrc.service.WechatService;
+import com.zoniot.ccrc.util.SpringContextUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.authentication.AuthenticationServiceException;
 import org.springframework.security.core.Authentication;
@@ -49,8 +50,8 @@ public class OpenidLoginAuthenticationFilter extends AbstractAuthenticationProce
         if (code == null) {
             code = "";
         }
-
-        String openid = WechatService.getOpenid(code,customerId);
+        WechatService wechatService= SpringContextUtil.getBean(WechatService.class);
+        String openid = wechatService.getOpenid(code,customerId);
 
 
         OpenidLoginAuthenticationToken authRequest = new OpenidLoginAuthenticationToken(openid);

+ 7 - 6
src/main/java/com/zoniot/ccrc/service/WechatService.java

@@ -31,12 +31,13 @@ public class WechatService {
 
 
     //水查查小程序
+    @Value("${wechat.mp.app-secret}")
+    private    String APP_SECRET="a01d7f75f15e1a6b7fa28a317f97ef1b";
+    @Value("${wechat.mp.appid}")
+    private    String APP_ID ;
 
-    private  static  String APP_SECRET="a01d7f75f15e1a6b7fa28a317f97ef1b";
 
-    private  static  String APP_ID="wx27f831675081e293" ;
-
-    public  static   String getSessionkey(String code) {
+    public     String getSessionkey(String code) {
         JSONObject oppidObj=getWechatAuthInfo(code);
         Integer errcode = (Integer) oppidObj.get("errcode");
         if(errcode == null){
@@ -49,7 +50,7 @@ public class WechatService {
         }
 
     }
-    private static  JSONObject getWechatAuthInfo(String code){
+    private   JSONObject getWechatAuthInfo(String code){
         String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + APP_ID + "&secret="
                 + APP_SECRET + "&js_code=" + code + "&grant_type=authorization_code";
         String reusult = null;
@@ -62,7 +63,7 @@ public class WechatService {
         JSONObject oppidObj = JSONObject.parseObject(reusult);
         return oppidObj;
     }
-    public static String getOpenid(String code,String customerId) {
+    public  String getOpenid(String code,String customerId) {
         JSONObject oppidObj=getWechatAuthInfo(code);
         Integer errcode = (Integer) oppidObj.get("errcode");
         if(errcode == null){

+ 36 - 0
src/main/java/com/zoniot/ccrc/util/SpringContextUtil.java

@@ -0,0 +1,36 @@
+package com.zoniot.ccrc.util;
+
+import org.apache.commons.lang3.ArrayUtils;
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author pengdi
+ */
+@Component
+public class SpringContextUtil implements ApplicationContextAware {
+
+    private static ApplicationContext context;
+
+    @Override
+    public void setApplicationContext(ApplicationContext applicationcontext) throws BeansException {
+        SpringContextUtil.context = applicationcontext;
+    }
+
+    public static Object getBean(String beanName) {
+        return context.getBean(beanName);
+    }
+
+    public static <T> T getBean(Class<T> clazz) {
+        String[] beanNames = context.getBeanNamesForType(clazz);
+        if (ArrayUtils.isEmpty(beanNames)) {
+            throw new IllegalArgumentException("There are no bean of type " + clazz.getName());
+        } else if (beanNames.length > 1) {
+            throw new IllegalArgumentException("There are more than one bean of type " + clazz.getName());
+        }
+        return (T) getBean(beanNames[0]);
+    }
+
+}