SpringCloud实战(5)SpringSecurity消费端访问

2025-11-06 23:42:42

1、    在实际开发中,对于Rest服务提供者是不能被用户直接访问的,于是肯定需要有一个Rest客户端(Web端,springboot),进行调用,可是Rest提供者的服务上了有了认证信息,那么该如何访问呢?

     如果这个时候在Rest客户端上直接使用用户名和密码做加密处理,那么根本就无法进行访问,此时会出现有401的错误代码,因为认证出现了错误。之所以无法访问,是因为所有的认证处理操作应该以头信息的模式来进行处理。而后我们要使用Base64进行加密处理后才可以得到一个正确的访问路径。

2、提供者进行了SpringSecurity安全处理,消费端直接访问会出现认证错误。

SpringCloud实战(5)SpringSecurity消费端访问

3、建议一个测试类TestHeader:

public class TestHeader {

public  static void main(String[] args) {

String autho = "gwolf:gwolf";

byte[] encodedAuth =  Base64.getEncoder().encode(

autho.getBytes();//进行一个加密处理

//在进行授权的头信息内容配置的时候加密的信息一定要与"Basic"之间有一个空格

String authHeader = "Basic " + new String(encodedAuth);

System.out.println(new String(encodedAuth));

}

}

SpringCloud实战(5)SpringSecurity消费端访问

SpringCloud实战(5)SpringSecurity消费端访问

4、【microcloud-consumer-80】修改RestConfig配置类,在这个类上配置新的Bean配置项。

@Configuration

public class RestConfig {

//要进行一个Http头信息配置

@Bean

public HttpHeaders getHeader() {

HttpHeaders httpHeaders = new HttpHeaders();

String autho = "gwolf:gwolf";

byte[] encodedAuth =  Base64.getEncoder().encode(

autho.getBytes(Charset.forName("US")));//进行一个加密处理

//在进行授权的头信息内容配置的时候加密的信息一定要与"Basic"之间有一个空格

String authHeader = "Basic " + new String(encodedAuth);//认证的原始信息

httpHeaders.set("Authorization",authHeader);

return httpHeaders;

}

@Bean

public RestTemplate getRestTemplate() {

return new RestTemplate();

}

}

SpringCloud实战(5)SpringSecurity消费端访问

5、【microcloud-consumer-80】修改ConsumerDeptController配置类,在进行Rest访问的时候设置好这个头部的信息。

@RestController

public class ConsumerDeptController {

    public static final String DEPT_GET_URL = "http://dept-8001.com:8001/dept/get/";

    public static final String DEPT_LIST_URL = "http://dept-8001.com:8001/dept/list/";

    public static final String DEPT_ADD_URL = "http://dept-8001.com:8001/dept/add";

    @Resource

    private RestTemplate restTemplate;

    @Resource

    private HttpHeaders httpHeaders;

    @RequestMapping(value = "/consumer/dept/get",method = RequestMethod.GET)

    public Object getDept(long id) {

        //Dept dept = this.restTemplate.getForObject(DEPT_GET_URL + id,Dept.class);

        Dept dept = this.restTemplate.exchange(DEPT_GET_URL + id, HttpMethod.GET,

                new HttpEntity<Object>(this.httpHeaders),Dept.class).getBody();

        return dept;

    }

    @RequestMapping(value = "/consumer/dept/list")

    public Object listDept() {

        List<Dept> allDepts = this.restTemplate.getForObject(DEPT_LIST_URL,List.class);

        return allDepts;

    }

    @RequestMapping(value = "/consumer/dept/add")

    public Object addDept(Dept dept) {

        return this.restTemplate.postForObject(DEPT_ADD_URL,dept,Boolean.class);

    }

}

6、访问:http://client.com/consumer/dept/get?id=1

SpringCloud实战(5)SpringSecurity消费端访问

7、【microcloud-consumer-80】修改ConsumerDeptController配置类,在进行Rest访问的时候设置好list方法这个头部的信息。

   @RequestMapping(value = "/consumer/dept/list")

    public Object listDept() {

        //List<Dept> allDepts = this.restTemplate.getForObject(DEPT_LIST_URL,List.class);

        List<Dept> allDepts = this.restTemplate.exchange(DEPT_LIST_URL,HttpMethod.GET,

                new HttpEntity<Object>(this.httpHeaders),List.class).getBody();

        return allDepts;

    }

SpringCloud实战(5)SpringSecurity消费端访问

8、【microcloud-consumer-80】修改ConsumerDeptController配置类,在进行Rest访问的时候设置好add方法这个头部的信息。

package com.gwolf.microcloud.controller;

import com.gwolf.vo.Dept;

import org.springframework.http.HttpEntity;

import org.springframework.http.HttpHeaders;

import org.springframework.http.HttpMethod;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

import java.util.List;

@RestController

public class ConsumerDeptController {

    public static final String DEPT_GET_URL = "http://dept-8001.com:8001/dept/get/";

    public static final String DEPT_LIST_URL = "http://dept-8001.com:8001/dept/list/";

    public static final String DEPT_ADD_URL = "http://dept-8001.com:8001/dept/add";

    @Resource

    private RestTemplate restTemplate;

    @Resource

    private HttpHeaders httpHeaders;

    @RequestMapping(value = "/consumer/dept/get",method = RequestMethod.GET)

    public Object getDept(long id) {

        //Dept dept = this.restTemplate.getForObject(DEPT_GET_URL + id,Dept.class);

        Dept dept = this.restTemplate.exchange(DEPT_GET_URL + id, HttpMethod.GET,

                new HttpEntity<Object>(this.httpHeaders),Dept.class).getBody();

        return dept;

    }

    @RequestMapping(value = "/consumer/dept/list")

    public Object listDept() {

        //List<Dept> allDepts = this.restTemplate.getForObject(DEPT_LIST_URL,List.class);

        List<Dept> allDepts = this.restTemplate.exchange(DEPT_LIST_URL,HttpMethod.GET,

                new HttpEntity<Object>(this.httpHeaders),List.class).getBody();

        return allDepts;

    }

    @RequestMapping(value = "/consumer/dept/add")

    public Object addDept(Dept dept) {

        return this.restTemplate.exchange(DEPT_ADD_URL,HttpMethod.POST,

                new HttpEntity<Object>(dept,this.httpHeaders),Boolean.class).getBody();

    }

}

SpringCloud实战(5)SpringSecurity消费端访问

9、对于Rest而言,实际上里面如果要进行参数的传递有各种方式,例如:各种页面的路径信息组成。如果要传递负责内容,建议你在整个处理的时候就去使用那些页面的参数传递的模式。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢