春季靴jwt

时间:2019-06-30 10:12:36

标签: spring-boot jwt

创建了2个项目

  1. Web API-使用jwt的其余API

  2. 网络应用-用于网络客户端

在网络应用中 控制器配置如下

用于从API项目获取令牌的代码。

@PostMapping("/")
        public String index(Model model){

ResponseEntity<String> response = null;
            System.out.println("Authorization Code------" );

            String encodedCredentials = new String(Base64.encodeBase64(credentials.getBytes()));

            HttpHeaders headers = new HttpHeaders();
            //headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
            headers.setContentType(MediaType.APPLICATION_JSON);
            //headers.add("Authorization", "Basic " + encodedCredentials);

            RestTemplate restTemplate = new RestTemplate();

            MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();     

            body.add("username", "test");
            body.add("password", "test");

            // Note the body object as first parameter!
            HttpEntity<?> request = new HttpEntity<Object>(body, headers);


            //HttpEntity<String> request = new HttpEntity<String>(headers);




            String access_token_url = "http://localhost:8097/authenticate";
            //access_token_url += "?code=" + code;
            //access_token_url += "&grant_type=authorization_code";
            //access_token_url += "&redirect_uri=http://localhost:8090/showEmployees";

            response = restTemplate.exchange(access_token_url, HttpMethod.POST, request, String.class);

            System.out.println("Access Token Response ---------" + response.getBody());

但是从Web api令牌调用中获得错误

  

错误   org.springframework.web.client.HttpClientErrorException $ BadRequest:   400 null at   org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:79)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:778)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:579)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   com.inity.webapp.controller.HomeController.index(HomeController.java:68)   〜[classes /:na]在sun.reflect.NativeMethodAccessorImpl.invoke0(本机   方法)〜[na:1.8.0_171]在   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)   〜[na:1.8.0_171]在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)   〜[na:1.8.0_171]在java.lang.reflect.Method.invoke(Method.java:498)   〜[na:1.8.0_171]在   org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)   〜[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)   〜[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)   〜[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)   〜[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)   〜[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)   〜[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)   〜[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)   〜[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   javax.servlet.http.HttpServlet.service(HttpServlet.java:660)   〜[tomcat-embed-core-9.0.21.jar:9.0.21]在   org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)   〜[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   javax.servlet.http.HttpServlet.service(HttpServlet.java:741)   〜[tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)   〜[tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)   〜[tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)   〜[tomcat-embed-websocket-9.0.21.jar:9.0.21]在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)   〜[tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)   〜[tomcat-embed-core-9.0.21.jar:9.0.21]在   org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)   〜[tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)   〜[tomcat-embed-core-9.0.21.jar:9.0.21]在   org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)   〜[tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)   〜[tomcat-embed-core-9.0.21.jar:9.0.21]在   org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)   〜[tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)   〜[tomcat-embed-core-9.0.21.jar:9.0.21]在   org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)   〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)   〜[tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)   〜[tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)   〜[tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)   [tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)   [tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)   [tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)   [tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)   [tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)   [tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)   [tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)   [tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:853)   [tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1587)   [tomcat-embed-core-9.0.21.jar:9.0.21]在   org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)   [tomcat-embed-core-9.0.21.jar:9.0.21]在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)   [na:1.8.0_171]在   java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)   [na:1.8.0_171]在   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)   [tomcat-embed-core-9.0.21.jar:9.0.21]在   java.lang.Thread.run(Thread.java:748)[na:1.8.0_171]

使用邮递员完美地工作

0 个答案:

没有答案