欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

毕设第二天

程序员文章站 2022-07-01 15:15:09
...

好久没有完整的程序了,上个完整的还是去年7月份,因为备战考研也没有学习新技术。
今天把登陆 注册 找回密码的后台程序完成了。利用阿里云的短信发送机制,拦截器等功能。

短信机制

package com.lzm.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Controller;

/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.0.3</version>
</dependency>
*/
@Controller
public class SendSms {

    public String PhoneMsg(String phone) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIP9rsh3YQnp4V", "EqazTiOwQlDz2T2BvTVEeLuXrywk8V");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "懒虫笔记");
        request.putQueryParameter("TemplateCode", "SMS_170347395");
        String s = vcode();
        request.putQueryParameter("TemplateParam", "{\"code\":\""+s+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return s;
    }


    /**
     * 生成6位随机数验证码
     * @return
     */
    public static String vcode(){
        String vcode = "";
        for (int i = 0; i < 6; i++) {
            vcode = vcode + (int)(Math.random() * 9);
        }
        return vcode;
    }

}

拦截器

package com.lzm.interceptor;


import com.lzm.pojo.UserInfo;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Interceptor1 implements HandlerInterceptor{

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		String requestURI = request.getRequestURI();
		//如何实现一个字符串是否包含数组中的任一元素
		if(!requestURI.contains("Login") ){
			UserInfo user = (UserInfo) session.getAttribute("user");
			if (null == user) {
//	/没有登陆,转向登陆界面
				request.getRequestDispatcher("/ToLogin.action").forward(request,response);
				return false;
			}
		}
		return true;
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("方法后 1");
		
	}
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("页面渲染后 1");
		
	}



}

拦截器需要在springmvc配置

		<!--&lt;!&ndash;SpringMVC拦截器&ndash;&gt;-->
<!--&lt;!&ndash;-->
			<!--&lt;!&ndash; 多个拦截器 &ndash;&gt;-->
	<!--<mvc:interceptors>-->
			<!--<mvc:interceptor>-->
				<!--<mvc:mapping path="/*.action"/>-->
				<!--&lt;!&ndash;&lt;!&ndash; 自定义的拦截器类 &ndash;&gt;&ndash;&gt;-->
				<!--<bean class="com.lzm.interceptor.Interceptor1"/>-->
			<!--</mvc:interceptor>-->
	<!--</mvc:interceptors>-->

感觉内容挺多的,明天把用户管理做了,一个一个来吧。
毕设第二天

相关标签: 毕业设计