码迷,mamicode.com
首页 > 其他好文 > 详细

SSM 签名加密+xml数据交互

时间:2019-05-20 19:50:26      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:url   web   erp   json   exce   发送   temp   factory   nps   

  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数据交互

标签:url   web   erp   json   exce   发送   temp   factory   nps   

原文地址:https://www.cnblogs.com/wlxslsb/p/10895516.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!