스프링 부트와 함께 작동하는 일부 응용 프로그램이 있습니다. 이것은 단지 로그인/등록 페이지입니다.JWT를 사용하여 Twitter 로그인을 통합하는 방법?
내 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에서 소비자와 비밀 키를 가져옵니다
- 는
- 설정 스프링 사회 dependecies application.properties에 넣어
- 내 HTML 로그인 페이지 파일 변경 SocialConfig 클래스를 생성
로그인 컨트롤러 클래스를 변경해야합니까?