SpringMVC+MongoDB+Maven整合(微信回調Oauth授權) 頂 原 薦

個人小程序。裏面是基於百度大腦 騰訊優圖做的人臉檢測。是關於人工智能的哦。

2017年第一篇自己在工作中的總結文檔。土豪可以打賞哦。

https://git.oschina.net/xshuai/smplat.git 項目在GIT上面了。這裏就是源代碼地址。麻煩看仔細點

  • 項目簡述:

使用SpringMVC+Maven搭建,整合MongoDB。入門級整理總結,不足之處多多理解。 

  • 項目結構:

 

  • XML相關配置

POM.XML

注意: SpringMVC使用的版本4.0.5 其他版本的請自行測試        

<!--增加mongodb也會自動增加mongojavadriver-->
<dependency>
     <groupId>org.springframework.data</groupId>
     <artifactId>spring-data-mongodb</artifactId>
     <version>1.2.0.RELEASE</version>
</dependency>
<!--相關驅動-->
 <dependency>  
      <groupId>org.mongodb</groupId>  
      <artifactId>mongo-java-driver</artifactId>  
      <version>2.10.1</version>  
</dependency>  

WEB.XML配置引入相關xml文件

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mvc.xml;classpath:mongodb-context.xml</param-value>
  </context-param>

SPRING-MVC.XML

注意: SpringMVC配置僅供參考哦

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
					http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
					http://www.springframework.org/schema/util
					http://www.springframework.org/schema/util/spring-util.xsd 
					http://www.springframework.org/schema/context 
					http://www.springframework.org/schema/context/spring-context-3.1.xsd 
					http://www.springframework.org/schema/mvc 
					http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
					http://www.springframework.org/schema/mvc/spring-mvc.xsd
					">

	<!-- 配置1: 自動掃描controller包下的所有類,使其認爲spring mvc的控制器 -->
	<context:component-scan base-package="com.bdxc" >
	    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
	  	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
	  	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />
  	</context:component-scan> 
	<!-- 配置靜態資源,直接映射到對應的文件夾,不被DispatcherServlet處理,3.04新增功能,需要重新設置spring-mvc-3.0.xsd -->
	<mvc:resources mapping="/images/**" location="/images/"/>
	<mvc:resources mapping="/js/**" location="/js/"/>
	<mvc:resources mapping="/css/**" location="/css/"/>
	<mvc:resources mapping="/swf/**" location="/swf/"/>
	<mvc:resources mapping="/file/**" location="/file/"/>
	<mvc:resources mapping="/FusionCharts/**" location="/FusionCharts/"/>
	<mvc:resources mapping="/bootstrap/**" location="/bootstrap/"/>
	<mvc:resources mapping="/uploads/**" location="/uploads/"/>
	<mvc:resources mapping="/signs/**" location="/signs/"/>
	<mvc:resources mapping="/anzhuangbao/**" location="/anzhuangbao/"/>
	<!-- 配置2: 避免IE執行AJAX時,返回JSON出現下載文件 -->
	<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
			</list>
		</property>
	</bean>
	 <!-- 配置3: 保證interceptor中通過handler獲得請求的method對象  -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" p:ignoreDefaultModelOnRedirect="true">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" />
			</list>
		</property>
	</bean>  
	<!-- 配:5:對模型視圖名稱的解析,即在模型視圖名稱添加前後綴 -->
<!-- viewResolver 視圖解析器,將視圖名(ModelAndView中的view)解析成URL-->
	 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  		
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="suffix" value=".jsp" />
		<property name="prefix" value="/WEB-INF/"/>
		<property name="order" value="20"></property>
		<property name="viewClass"
			value="org.springframework.web.servlet.view.InternalResourceView" />
	</bean>

	<!-- 配置6: 配置Spring自帶文件上傳操作類 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding">
	      <value>UTF-8</value>
		</property>
		<property name="maxUploadSize">
	        <value>2147483648</value>
                 <!-- 上傳文件大小限制爲31M,31*1024*1024 -->
		</property>
		<property name="maxInMemorySize">
			<value>4096</value>
		</property>
	</bean>
	
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**" />
			<bean class="com.bdxc.plat.system.interceptors.EncodingInterceptor" />
		</mvc:interceptor>
		<mvc:interceptor>
			<mvc:mapping path="/" />
			<bean class="com.bdxc.plat.system.interceptors.AuthInterceptor" />
		</mvc:interceptor>
	</mvc:interceptors>
		<!-- 國際化配置 -->  
	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" >
		<property name="cookieName" value="clientlanguage"/>
		<property name="cookieMaxAge" value="94608000"/>
	</bean>  
</beans>

MONGODB-CONTEXT.XML

這塊會涉及到mongodb是否驗證用戶名及密碼的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mongo="http://www.springframework.org/schema/data/mongo"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	 
	 <!-- 加載mongodb的屬性配置文件 -->
	<context:property-placeholder location="classpath:config.properties" /> 
	
	<!-- 定義mongo對象,對應的是mongodb官方jar包中的Mongo,replica-set設置集羣副本的ip地址和端口 -->
	<mongo:mongo id="mongo" replica-set="${mongo.hostport}" > 
         <!-- 一些連接屬性的設置 -->
		<mongo:options 
			connections-per-host="${mongo.connectionsPerHost}"
			threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
			connect-timeout="${mongo.connectTimeout}" 
			max-wait-time="${mongo.maxWaitTime}"
			auto-connect-retry="${mongo.autoConnectRetry}" 
			socket-keep-alive="${mongo.socketKeepAlive}"
			socket-timeout="${mongo.socketTimeout}" 
			slave-ok="${mongo.slaveOk}"
			write-number="1"
			write-timeout="0"
			write-fsync="true" />
	</mongo:mongo> 
	<mongo:db-factory dbname="${mongo.dbname}" mongo-ref="mongo"/> 
	<!-- 增加驗證如果沒有的話這塊可以註釋 -->
	<bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
		<constructor-arg name="username" value="${mongo.username}"></constructor-arg>
		<constructor-arg name="password" value="${mongo.password}"></constructor-arg>
	</bean>
	<!-- mongo的工廠,通過它來取得mongo實例,dbname爲pe的數據庫名,沒有的話會自動創建-->
        <!-- 可以點擊class查看MongoTemplate類提供的 構造方法就可以看出驗證需要傳遞哪些參數 -->
	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
		<constructor-arg name="mongo" ref="mongo" />
		<constructor-arg name="databaseName" value="${mongo.dbname}" />
                <!-- 增加驗證如果沒有的話這塊可以註釋 -->
		<constructor-arg name="userCredentials" ref="userCredentials"/>
	</bean>
	 <!-- 映射轉換器,掃描back-package目錄下的文件,根據註釋,把它們作爲mongodb的一個collection的映射 -->
	<mongo:mapping-converter base-package="com.bdx.plat.model" /> 
	<!-- mongodb bean的倉庫目錄,會自動掃描擴展了MongoRepository接口的接口進行注入 -->
	<mongo:repositories base-package="com.bdxc" />
</beans>
  • 相關的java內容,看源代碼的構造和api可以有很大幫助哦

MongoTemplate.java可以看到有2個構造方法這樣就根據自己的MongoDB是否有驗證進行xml配置

	/**
	 * Constructor used for a basic template configuration
	 * 
	 * @param mongo must not be {@literal null}.
	 * @param databaseName must not be {@literal null} or empty.
	 */
	public MongoTemplate(Mongo mongo, String databaseName) {
		this(new SimpleMongoDbFactory(mongo, databaseName), null);
	}

	/**
	 * Constructor used for a template configuration with user credentials in the form of
	 * {@link org.springframework.data.authentication.UserCredentials}
	 * 
	 * @param mongo must not be {@literal null}.
	 * @param databaseName must not be {@literal null} or empty.
	 * @param userCredentials
	 */
	public MongoTemplate(Mongo mongo, String databaseName, UserCredentials userCredentials) {
		this(new SimpleMongoDbFactory(mongo, databaseName, userCredentials));
	}

UserCredentials.java 查看驗證傳遞的參數 username & password

public class UserCredentials {

	public static final UserCredentials NO_CREDENTIALS = new UserCredentials(null, null);

	private final String username;
	private final String password;

	/**
	 * Creates a new {@link UserCredentials} instance from the given username and password. Empty {@link String}s provided
	 * will be treated like no username or password set.
	 * 
	 * @param username
	 * @param password
	 */
	public UserCredentials(String username, String password) {
		this.username = StringUtils.hasText(username) ? username : null;
		this.password = StringUtils.hasText(password) ? password : null;
	}

整合MongoDB的內容就這些。後續會將整個代碼及項目上傳git,訪問路徑會在此博文更新

以下內容是小測試。

基於該框架寫了一個微信的回調和授權(Oauth),SpringMVC+MongoDB+Maven搭建微信後臺框架,包含了回調配置和授權Oauth配置 項目結構在最上面有截圖哦。

#基於SpringMVC+MongoDB數據庫做的微信接入等一些常用接口的DEMO
只實現了回調 和oauth 接口
1. common存放相關基礎代碼和微信常量
1.1 com/bdxc/plat/common/weixin/WXConstants.java 修改爲自己的微信相關的APPID APPSERCET
1.2 com.bdxc.plat.controller存放爲請求訪問層代碼
1.2.1 WXConfigController.java 回調配置需要用到,GET爲回調。POST 爲用戶發送信息進行處理並返回
1.2.2 WXOauthController.java Oauth授權獲取用戶信息的代碼

2. com.bdxc.plat.vo 存放微信相關的接口基礎對象

3.com.bdxc.plat.util 存放相關工具類代碼包含微信需要用到的  
3.1 com.bdxc.plat.util.weixin 存放微信相關工具類

4.com.bdxc.plat.service.weixin 存放微信用戶給公衆發送信息進行處理的方法, 上一級爲操作數據的service 不保存不需要關注

5.com.bdxc.plat.model.weixin 存微信的消息類型的對象,上一級爲數據庫的model  不保存不需要關注
  • 回調配置代碼實現
/**
 * 回調配置
 * @author 宗瀟帥
 * @Title WXConfigController
 * @時間   2017-1-4上午11:06:05
 */
@Controller
@RequestMapping(value="/wxconfig")
public class WXConfigController {
	private static Logger logger = Logger.getLogger(WXConfigController.class);
	
	@RequestMapping(value="/valid",method={RequestMethod.GET},produces = "application/json;charset=UTF-8")
	public void valid(HttpServletRequest request,HttpServletResponse response,PrintWriter out) throws Exception{
		System.out.println("回調請求=======================");
		//微信加密簽名
		String signature = request.getParameter("signature");
		//時間戳
		String timestamp = request.getParameter("timestamp"); 
		//隨機數
		String nonce = request.getParameter("nonce");
		//隨機字符串
		String echostr = request.getParameter("echostr");
		out = response.getWriter();
		//通過檢驗signature對請求進行校驗,若校驗成功則原樣返回echostr,表示接入成功,否則接入失敗 
		if(SignUtil.checkSignature(signature, timestamp, nonce)){
			out.print(echostr);
		}else{
			System.out.println("非微信發送的GET請求");
		}
		logger.info("回調請求發送的參數爲signature"+signature+"\n"+"timestamp"+timestamp+"\n"+"nonce"+nonce+"\n"+"echostr"+echostr);
		out.flush();
		out.close();
	}
	@RequestMapping(value="/valid",method={RequestMethod.POST},produces = "application/json;charset=UTF-8")
	public void infos(HttpServletRequest request,HttpServletResponse response,PrintWriter out) throws Exception{
        /* 消息的接收、處理、響應 */  
		  
        // 將請求、響應的編碼均設置爲UTF-8(防止中文亂碼)  
        request.setCharacterEncoding("UTF-8");  
        response.setCharacterEncoding("UTF-8");  
        CoreService coreService = new CoreService();
        // 調用核心業務類接收消息、處理消息  
        String respMessage = coreService.processRequest(request);  
        logger.info(respMessage);  
        // 響應消息  
        out = response.getWriter();  
        out.print(respMessage);  
        out.close(); 
	}
}
  •  StringUtil代碼
/**
 * 
 * 請求校驗工具類 
 *
 */
public class SignUtil {
	/** 
	 * 驗證簽名 
	 *  
	 * @param signature 
	 * @param timestamp 
	 * @param nonce 
	 * @return 
	 */
	public static boolean checkSignature(String signature, String timestamp,
			String nonce) {
		String[] arr = new String[] {WXConstants.TOKEN, timestamp, nonce };
		// 將token、timestamp、nonce三個參數進行字典序排序  
		Arrays.sort(arr);
		StringBuilder content = new StringBuilder();
		for (int i = 0; i < arr.length; i++) {
			content.append(arr[i]);
		}
		MessageDigest md = null;
		String tmpStr = null;

		try {
			md = MessageDigest.getInstance("SHA-1");
			// 將三個參數字符串拼接成一個字符串進行sha1加密  
			byte[] digest = md.digest(content.toString().getBytes());
			tmpStr = byteToStr(digest);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		content = null;
		// 將sha1加密後的字符串可與signature對比,標識該請求來源於微信  
		return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
	}
}

 免費的外網映射https://my.oschina.net/xshuai/blog/597760

  • Oauth接口,微信授權登錄的代碼實現
/**
 * oauth獲取用戶信息並保存到mongodb
 * @author 宗瀟帥
 * @Title WXOauthController
 * @時間   2017-1-4上午11:06:14
 */
@Controller
@RequestMapping(value="/wx")
public class WXOauthController {
	
	
	private static Logger logger = Logger.getLogger(WXOauthController.class);
	@Autowired
	private WXUserInfoService wxUserInfoService;
	/**
	 * oauth獲取用戶相關信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/oauth",method={RequestMethod.POST,RequestMethod.GET})
	public String oauth(HttpServletRequest request,HttpServletResponse response) throws Exception{
			//接受參數
			HttpSession httpSession = request.getSession();
			String code = request.getParameter("code");
			String scope = request.getParameter("scope");
			logger.info("==============[OAuthServlet]獲取網頁授權code="+code);
			logger.info("==============[OAuthServlet]獲取網頁跳轉權限="+scope);
			if(null != code && !"".equals(code)){
				logger.info("==============[OAuthServlet]獲取網頁授權code不爲空,code="+code);
				//根據code換取openId
				OAuthInfo oa = WeixinUtil.getOAuthOpenId(WXConstants.appId,WXConstants.appSecret,code);
				//第一次獲取到存到session裏面 防止用戶刷新頁面
				if(oa!=null){
					httpSession.setAttribute("openid", oa.getOpenId());
				}else{
					//如果用戶是刷新頁面。則讀取session的openid 
					Object openid = httpSession.getAttribute("openid");
					if(openid!=null){
						OAuthInfo authInfo = new OAuthInfo();
						String openids = openid.toString();
						System.out.println("刷新頁面留存的openid"+openids);
						authInfo.setOpenId(openids);
						oa = authInfo;
					}else{
						//session也爲空 建議用戶重新進入頁面
//						request.getRequestDispatcher("/warn.jsp").forward(request, response);
						return "";
					}

				}
				AccessToken oasign = WXConstants.ACCESS_TOKEN;
				WXUserInfo info = WeixinUtil.getWXUserInfo(oasign.getToken(), oa.getOpenId());
				if(!"".equals(oa) && null != oa){
					logger.info("==============[OAuthServlet]獲取網頁授權openID="+oa.getOpenId());
					//保存信息
					try {
						wxUserInfoService.insert(info);
						request.setAttribute("openid", oa.getOpenId());
						request.setAttribute("nickname", info.getNickname());
						request.setAttribute("headimgurl", info.getHeadimgurl());
						request.setAttribute("city", info.getCity());
						request.getRequestDispatcher("/index.jsp").forward(request, response);
						return null;
					} catch (Exception e) {
						e.printStackTrace();
						logger.info("保存失敗"+e.getMessage());
					}
				}else{
					logger.info("==============[OAuthServlet]獲取網頁授權openId失敗!");
				}
			}else{
				logger.info("==============[OAuthServlet]獲取網頁授權code失敗!");
			}
			return null;
	}
}

 基於mongodb做了一個測試,授權登錄的用戶進行將相關信息保存。簡單實現沒有做任何封裝。使用的是MongoTemplate這個類進行的增刪改查。

測試號關注超過100人就會有問題。因此我全部移除了,想看效果的重新關注測試號,demo的框架換成了上面使用的SpringMVC+MongoDB+Maven整合(微信回調Oauth授權),因此有些菜單點擊會有錯誤哦。

第一訪問會提示確認登錄的相關信息。確認登錄後就是右邊顯示的相關內容哦。

                 

確認登錄後獲取到openid,那就可以拿到用戶的相關信息了。存在了mongodb數據庫

mongodb是documents 非關係型數據庫。保存的都是以文本 大家可以理解爲json字符串

得到的數據如下面代碼顯示。id是mongodb生成的唯一id

{
  "_id" : ObjectId("586cbbe7150f14811ce04546"),
  "_class" : "com.bdxc.plat.model.WXUserInfo",
  "subscribe" : 1,
  "openid" : "o2VKNju8JqCeGVoEWJ1S8Ue_up8E",
  "nickname" : "小帥丶",
  "sex" : 1,
  "language" : "zh_CN",
  "city" : "海淀",
  "province" : "北京",
  "country" : "中國",
  "headimgurl" : "http://wx.qlogo.cn/mmopen/Lj9cibm6LlmjNM8CGSYKuMQiaD4tTPwUjD7zVkkn2u6kFqv4zDwtcfHFntHyxtjjmXeicLDqVqQB42vUukxB5Mia8HgoV94gsN02/0",
  "subscribe_time" : "1483520921",
  "unionid" : "oUmIot2Yo2Mb_8fVW3UVw9AW1w4Y",
  "remark" : "",
  "groupid" : 0,
  "tagid_list" : []
}

本文主要是說mongodb驗證用戶名和密碼的xml的配置內容。順便博主拿微信寫了個demo。後續完善後會上傳git。目前有servlet的版本在衆包提供服務哦。https://zb.oschina.net/service/f9918c2f1e643513

土豪可以打賞哦。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章