JWT

2017-11-01 3 views
1
와 백엔드 봄에 각도 클라이언트 로그인을 확인

나는이 가이드 다음 내 봄 백엔드에 JWT를 추가 : https://auth0.com/blog/securing-spring-boot-with-jwts/JWT

나는 즉시 난으로 우편 배달 모든 것이 잘 작동과 같은 소프트웨어를 사용하여 PUT 요청하지만를 보낼 때 의 데이터가 내 Angular 클라이언트에 로그인하려고 시도하면 비어 있습니다.

@Override 
public Authentication attemptAuthentication(HttpServletRequest req, HttpServletResponse res) 
     throws AuthenticationException, IOException, ServletException { 
    String reqBody = req.getReader().lines().collect(Collectors.joining(System.lineSeparator())); 

    // this appears to be empty on angular client calls 
    System.out.println(reqBody); 

    ObjectMapper objectMapper = new ObjectMapper().configure(Feature.AUTO_CLOSE_SOURCE, true) 
      .enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); 
    AccountCredentials creds = objectMapper.readValue(reqBody, AccountCredentials.class); 

    return getAuthenticationManager().authenticate(new UsernamePasswordAuthenticationToken(creds.getUsername(), 
       creds.getPassword(), Collections.emptyList())); 
} 

이 같은 클라이언트로부터 요청을 보내고있다 :

const user = { 
    username: "asdf", 
    password: "asdf" 
}; 

// imported from '@angular/http' 
const headers = new Headers({ 
    'Content-Type': 'application/json' 
}); 
const body = JSON.stringify(user); 
return this.http 
    .put("http://localhost:8080/api/login", body, {headers}) 
    .toPromise() 
    .then(response => response.json().data as User) 
    .catch(this.handleError); 

나의 제안 것

나는 다음과 같은 방법으로 JWTLoginFilterattemptAuthentication 방법으로 데이터를 확인 요청 본문을 잘못된 방법으로 보냈지 만, 내가 뭘 잘못하고 있는지 알 수 없습니다.

는 I 시도 대신 POST을 사용하여 실시 예

  • 에 도시

    • 그것이 다른 목적 JSON 문자열로 전송
    • 래핑 송신 일반 JS 객체
    • 으로 몸을 보내는 (PUT의 PUT과 작동 함에도 불구하고)
    • Content-Type 헤더를 다른 값으로 변경

    이 데이터가 백엔드에 표시되지 않았습니다.

    더 자세한 정보가 필요하면 제게 문의하십시오.

  • +0

    당신은'spring-security-oauth2' 의존성을 사용하고 있습니까? –

    +0

    @PraveenM 아니요, 저는'spring-boot-starter-security @ 1.5.8.RELEASE'와'jjwt @ 0.7.0'을 사용하고 있습니다. – Froxx

    답변

    0

    이것은 스프링 OAuth 2가 일반적으로 POST URL 요청 인 사용자 이름과 비밀번호로 요청할 것으로 예상하는 이유 일 수 있습니다. 그래서 이런 것을 시도해보십시오.

    return this.http.post(Url, "grant_type=password&username=" + username + "&password=" + password) 
    

    나는 나의로이 경우는 Spring-MVC 인 경우 100 % 모르겠지만, 나는 그것이 매우 유사 할 것으로 예상된다.

    +0

    이것은 스프링 일뿐만 아니라 일반적인 것이어야합니다. 따라서 Angular의 http 모듈이 이미이 문제에 신경을 써야합니다. 각도 홈 페이지의 자습서에 게시 및 전화를 걸면 적어도 본문 데이터가 JS 객체로 전달됩니다. – Froxx

    +0

    이는 Spring OAuth 구성이 무엇인지에 따라 다릅니다. Postman을 사용할 때 POST 본문이나 URL에 Username과 Password를 보내십니까? –

    +0

    본문의 원시 JSON 문자열로 전송합니다. – Froxx

    2

    알겠습니다.

    나는 다음과 같은도 내 HttpSecurity 객체에 CORS를 허용하는 데 필요한 :

    @Configuration 
    @EnableWebSecurity 
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
        @Override 
        protected void configure(HttpSecurity http) throws Exception { 
        http.cors() 
         .and() 
         .so() 
         .on(); 
        } 
    } 
    

    난 정말 모르겠어요, 내가 우체부와 함께 전송 요청이 내 HttpSecurity에서 활성화 CORS없이 수신받을 수있는 이유는, 그럼에도 불구하고 지금 일하고있어.

    앞으로이 문제로 다른 사람들을 도울 수 있기를 바랍니다.