簡體   English   中英

如何針對Web上的Spring Security使用Jersey客戶端進行身份驗證

[英]How authenticate using Jersey Client against a Spring Security on WS

我在澤西島上擁有此conf的Web服務以確保安全(春季):

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <!-- To allow public access by default and to set authentication mode to 
        basic login/password -->
    <security:global-method-security secured-annotations="enabled"/>

    <security:http>
        <security:http-basic/>
        <security:intercept-url pattern="/**" access="ROLE_DUMMY"/>
    </security:http>

    <!-- To delegate authorization to method calls rather than to urls -->
    <!-- (Thus, we don't need to set any url-interceptor in this conf) 
    <security:global-method-security
        pre-post-annotations="enabled" />-->

    <!-- To create user/password with roles -->
    <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider>
            <security:user-service>
                <security:user authorities="ROLE_DUMMY" name="user1"
                    password="strongpassword1" />
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>
</beans>

現在,我嘗試創建一個用於模擬客戶端的Java項目,因此我將客戶端球衣框架用於:

ClientConfig config = new ClientConfig();


        Client client = ClientBuilder.newClient(config);


        WebTarget target = client.target(getBaseURI());

        System.out.println(target.path("general").path("getWeather").request().request().header("Authorization: ", "Basic " + "dXNlcjE6c3Ryb25ncGFzc3dvcmQx")
 .accept(MediaType.APPLICATION_JSON).get(Response.class)
.toString());

但是我有錯誤:status = 401,原因=未授權,如何設置授權?

我解決:

String authorization = "user1" + ":" + "strongpassword1";
String basic = new String(Base64.encodeBase64(authorization.getBytes(Charset.forName("US-ASCII"))));

System.out.println(target.path("generali").path("getMeteo").request().header("Authorization", "Basic " + basic).accept(MediaType.APPLICATION_JSON).get(String.class));

暫無
暫無

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

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