SpringMVC+MongoDB+Maven整合(微信回调Oauth授权)

时间:2022-05-03
本文章向大家介绍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

土豪可以打赏哦。