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

Java Https Token认证,后端处理

程序员文章站 2023-12-21 18:25:16
...

1、测试场景

Java Https Token认证,后端处理2、传递的参数为

{     "servicename":"bweb_mind"
}

3、服务端验证代码为

  @RequestMapping(value = "/jenkinsconfirm/testtoken.do", method = RequestMethod.POST)
    @ResponseBody
    public String testtoken(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject obj) throws Exception {

       // String sessionAuth = (String) request.getSession().getAttribute("auth");
       // if (sessionAuth != null) {
        //    System.out.println("this is next step");
        //    nextStep(request, response);

       // } else {

            if(!checkHeaderAuth(request, response)){
                response.setStatus(401);
                response.setHeader("Cache-Control", "no-store");
                response.setDateHeader("Expires", 0);
                response.setHeader("WWW-authenticate", "Basic Realm=\"test\"");
                return "fail";
            }

        JSONObject jsonObject = JSONObject.fromObject(obj.toString());
            String name=jsonObject.getString("servicename");
            if(name.equals("bweb_mind")){
            return "success";}
            else return "servicename错误";

      //  }


    }

    private boolean checkHeaderAuth(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String auth = request.getHeader("Authorization");
        System.out.println("auth encoded in base64 is " + getFromBASE64(auth));

        if ((auth != null) && (auth.length() > 6)) {
            auth = auth.substring(6, auth.length());
            if(auth.equals("08*********"));

         //   String decodedAuth = getFromBASE64(auth);
         //   System.out.println("auth decoded from base64 is " + decodedAuth);

         //   request.getSession().setAttribute("auth", decodedAuth);
            return true;
        }else{
            return false;
        }

    }

 

相关标签: java认证

上一篇:

下一篇: