簡體   English   中英

將權限和用戶主體從 rest 客戶端傳遞到服務器 spring 啟動

[英]Passing authorities and user principal from rest client to server spring boot

我必須從 rest 客戶端調用一個安全端點,在 controller 端,它需要從客戶端發送權限和用戶主體信息。

 String  endpoint="http://localhost:8096/polygons/34";
           // endpoint="https://dop-int.edosdp.ericsson.se/polygon-manager/polygons/34";
            HttpHeaders headers = new HttpHeaders();
            headers.setBasicAuth("mahi", "ChangeM6");
            headers.setConnection("keep-alive");       
             HttpEntity<String> httpEntity = new HttpEntity<String>(headers);       
            ResponseEntity<Long> exchange = restTemplate.exchange(endpoint,HttpMethod.GET, httpEntity, Long.class);    

如何從客戶端發送至少一個角色(ADMIN 或 GUEST_USER)信息。 有什么辦法可以將所有用戶信息包裝在虛擬 session 中並將其發送給服務器。

謝謝,麻希

不可以。客戶端修改任何類型的 session 信息(包括 cookies)都是一個壞主意。只應允許服務器這樣做。

由於您的要求是檢查特定 url 上的用戶角色,您可以設置自定義請求 header 並在 controller 方法本身中檢查它:

示例代碼:

@GetMapping("/polygons")
public String getPolygons(HttpServletRequest request) {     
    String userRole = request.getHeader("user-role");
    if(userRole != null && userRole.toLowerCase().equals("admin")) {
        System.out.print("Role provided: " + userRole);     
        // ...
        return "some-data";
    }           
    
    System.out.print("Role not provided!");
    return "error";
}

郵遞員客戶端 - 設置自定義標題

您還可以在請求正文中為發布請求設置用戶角色。

public class RequestParams {
    private String userRole;
    // ...
}       

@PostMapping("/polygons")
public String getPolygons(@RequestBody RequestParams requestParams) {
    String userRole = requestParams.getUserRole();
    if(userRole != null && userRole.toLowerCase().equals("admin")) {
        System.out.print("Role provided: " + userRole);     
        // ...
        return "some-data";
    }           
    
    System.out.print("Role not provided!");
    return "error";
}

設置 POST 請求體

如果您的要求是檢查多個 url 上的用戶角色,那么您應該考慮編寫一個 servlet 過濾器。

編輯:

我想我過去也遇到過類似的情況。 我最終使用了 apache 的 httpclient 庫而不是 resttemplate。

這是一些示例代碼:

private List<OrganizationDTO> getUserOrgUnits(String loggedInUserId, String token) {
    List<OrganizationDTO> userList = new ArrayList<OrganizationDTO>();      

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(getUserOrgUnitsApiURL());
    try {
        // Setting header
        httpGet.setHeader("Authorization", "Bearer " + token);
        httpGet.setHeader("Content-type", "application/json");
        // Setting custom header
        httpGet.setHeader(USERID_HEADER_NAME, loggedInUserId);
        
        CloseableHttpResponse response = httpClient.execute(httpGet);
        String  result = EntityUtils.toString(response.getEntity());
        
        JsonNode node = null;
        ObjectMapper mapper = new ObjectMapper();
        node = mapper.readTree(result);
        Iterable<JsonNode> list = node.path("data");            
        for (JsonNode jsonNode : list) {
            OrganizationDTO dto = mapper.treeToValue(jsonNode, OrganizationDTO.class);
            userList.add(dto);
        }
    } catch (Exception e) {
        log.error("getUserOrgUnits: Exception.");
        e.printStackTrace();
    }                                       
    return userList;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM