2017-11-15 16 views
1

스프링 부트와 함께 작동하는 일부 응용 프로그램이 있습니다. 이것은 단지 로그인/등록 페이지입니다.JWT를 사용하여 Twitter 로그인을 통합하는 방법?

Start Login Register

내 JWT 필터

public class JWTFilter extends GenericFilterBean { 

    private static final String AUTHORIZATION_HEADER = "X-CustomToken"; 
    private static final String AUTHORITIES_KEY = "roles"; 

    /** 
    * doFilter method for creating correct token(Bearer has taken from front-end part) 
    * 
    * @param req 
    * @param res 
    * @param filterChain 
    * @throws IOException 
    * @throws ServletException 
    */ 
    @Override 
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) 
      throws IOException, ServletException { 
     HttpServletRequest request = (HttpServletRequest) req; 
     String authHeader = request.getHeader(AUTHORIZATION_HEADER); 
     if (authHeader == null || !authHeader.startsWith("Bearer ")) { 
      ((HttpServletResponse) res).sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid Authorization header."); 
     } else { 
      try { 
       String token = authHeader.substring(7); 
       Claims claims = Jwts.parser().setSigningKey("secretkey").parseClaimsJws(token).getBody(); 
       request.setAttribute("claims", claims); 
       SecurityContextHolder.getContext().setAuthentication(getAuthentication(claims)); 
       filterChain.doFilter(req, res); 
      } catch (SignatureException e) { 
       ((HttpServletResponse) res).sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid token"); 
      } 

     } 
    } 

    /** 
    * Method for creating Authentication for Spring Security Context Holder 
    * from JWT claims 
    */ 
    private Authentication getAuthentication(Claims claims) { 
     List<SimpleGrantedAuthority> authorities = new ArrayList<>(); 
     List<String> roles = (List<String>) claims.get(AUTHORITIES_KEY); 
     for (String role : roles) { 
      authorities.add(new SimpleGrantedAuthority(role)); 
     } 
     User principal = new User(claims.getSubject(), "", authorities); 
     UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
       principal, "", authorities); 
     return usernamePasswordAuthenticationToken; 
    } 
} 

내 WebSecurityConfig 클래스 그래서 1 관리자가 data.sql과에 등록하는 모든 사용자에 하드 코딩 한

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    /** 
    * This method is for overriding some configuration of the WebSecurity 
    * If you want to ignore some request or request patterns then you can 
    * specify that inside this method 
    */ 
    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web.ignoring() 
       // ignoring the "/index.html", "/app/**", "/registration", 
       // "/favicon.ico" 
       .antMatchers("/", "/index.html", "/app/**", "/registration", "/login", "/favicon.ico ", "/confirmRegistration/**") 
       .and(); 
       } 

    /** 
    * This method is used for override HttpSecurity of the web Application. 
    * We can specify our authorization criteria inside this method. 
    */ 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .authorizeRequests() 
       .anyRequest().fullyAuthenticated().and() 
       .addFilterBefore(new JWTFilter(), UsernamePasswordAuthenticationFilter.class) 
       .httpBasic().and() 
       .csrf().disable(); 
    } 
} 

내 응용 프로그램은 사용자 역할을 얻고 MySQL에 저장됩니다. Twitter, Facebook 및 Google과 같은 소셜 앱 로그인을 추가하고 싶습니다. JWT를 사용한다면 어떻게해야할까요? 내가 아는 한, Oauth2는 소셜 앱을 사용하는 것이 더 좋지만 내 방식으로 할 수 있습니까?

Twitter로 시작하려면 어떻게해야합니까? 트위터 API에서 소비자와 비밀 키를 가져옵니다

  1. 설정 스프링 사회 dependecies application.properties에 넣어
  2. 내 HTML 로그인 페이지 파일 변경 SocialConfig 클래스를 생성

로그인 컨트롤러 클래스를 변경해야합니까?

답변

0

다른 소셜 네트워크에 대해서는 잘 모르겠지만 코드가 Java이고 Twitter API로 작업 할 계획이라면 이동 방법은 Twitter4J library입니다. 이것은 자바 세계에서 사실상의 표준이며 잘 문서화되어 있습니다.

물론 트위터 개발자 계정이 필요합니다. Twitter Dev Portal

에 대한 자세한 정보를 얻을 수 있습니다.