SSM 签名加密+xml数据交互

Posted wlxslsb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM 签名加密+xml数据交互相关的知识,希望对你有一定的参考价值。

  Test.java

package com.controller;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;

import javax.swing.text.html.FormSubmitEvent.MethodType;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.HmacUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.pojo.Apple;
import com.pojo.UserInfo;

@Controller
public class Test {
    @Autowired
    RestTemplate rt;
    
    public void setRt(RestTemplate rt) {
        this.rt = rt;
    }
    @RequestMapping(value="/send",method=RequestMethod.GET)
    public @ResponseBody UserInfo send(){
        UserInfo user = new UserInfo();
        user.setUsername("张三");
        user.setPwd("123456");
        
        //发送xml类型的数据参数
        HttpHeaders headers = new HttpHeaders();
        //指定当前urlAPi发送的数据类型
        headers.setContentType(MediaType.APPLICATION_JSON);//MediaType.APPLICATION_XML支持xml
        //指定期待的相应数据类型
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        HttpEntity<UserInfo> entity = new HttpEntity<UserInfo>(user, headers);

        Apple apple = rt.postForObject("http://localhost:8888/SSM/get", user, Apple.class);
        System.out.println(apple.getAppleName());

        return user;
    }

    @RequestMapping("/get")
    public @ResponseBody Apple get(@RequestBody UserInfo user){
        System.out.println("name="+user.getUsername());
        Apple a = new Apple();
        a.setAppleName("苹果8");
        return a;
    }
    
    //密码加密测试
    public static void main(String[] args) throws UnsupportedEncodingException {
        //1.md5的加密方式  带key  和不带key
//        String pwd = "admin";
//        //不带key
//        String md5_32 = DigestUtils.md5Hex(pwd);
//        //带key
//        String md5_key = new HmacUtils(HmacAlgorithms.HMAC_MD5, "123").hmacHex(pwd);
//        System.out.println(md5_key);
        
        //2.spring-security 加密密码  优势:同一明文每次加密后的密文是不一样的
        BCryptPasswordEncoder bc = new BCryptPasswordEncoder();
        String pwd = bc.encode("admin");
        System.out.println(pwd);
        System.out.println(bc.matches("admin", "$2a$10$AvOUjKuluUaacjpTMKUDbeRs/7psaFqR24ATQtu0gmmQU3AnPSolK"));
    }
    
}

 

其他:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="js/jquery-3.2.1.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("a").click(function(){
            $.ajax("user/testjson",{
                method:get,
                dataType:xml,
                success:function(data){
                    console.log(data);
                }
            });
        });
    });
</script>
<body>
    <a>测试</a>
</body>
</html>

 

package com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pojo.UserInfo;
import com.service.IUserService;

@Controller
public class UserController {
    @Autowired
    IUserService userSer;
    
    public void setUserSer(IUserService userSer) {
        this.userSer = userSer;
    }

    @RequestMapping("/user/getall")//该方法的访问路径
    public ModelAndView getall(){
        System.out.println("oks");
        List<UserInfo> list = userSer.getAllUser();
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:/jsp/test.jsp");
        mv.addObject("list",list);
        return mv;
    }
    
    @RequestMapping("user/test")
    public String test(UserInfo user){
        System.out.println("okooooo");
        System.out.println(user.getUsername());
        return "jsp/test";
    }
    @RequestMapping(value="user/testjson",method=RequestMethod.GET)
    public @ResponseBody UserInfo testJson(){
        UserInfo user = new UserInfo();
        user.setUserid(1);
        user.setUsername("张三");
        user.setPwd("admin");
        return user;
    }
}

 

以上是关于SSM 签名加密+xml数据交互的主要内容,如果未能解决你的问题,请参考以下文章

基础篇:java.security框架之签名加密摘要及证书

使用带有签名、加密和编码的 JAXB

支付宝支付加密规则梳理,写的太好了!

支付宝支付加密规则梳理,写的太好了!

使用RSA算法对接口参数签名及验签

如何写出安全的API接口?接口参数加密签名设计思路