좋은 하루,봄 부팅 OAuth2Client 난에서 실행되는 스프링 부팅 응용 프로그램을
페이스 북의 로그인을 처리. 기본 기능 - "login/facebook"요청을 처리하고 거기에 적절한 로그인을하십시오. 동일한 도메인 (예 : http://localhost:8080/help 페이지)에서 요청이 전송되면 제대로 작동합니다.
그것은 방식으로 구현됩니다
@Configuration
@EnableOAuth2Client
public class SclLoginSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private OAuth2ClientContext oauth2ClientContext;
@Bean
public FilterRegistrationBean oauth2ClientFilterRegistration(
OAuth2ClientContextFilter filter) {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(filter);
registration.setOrder(-100);
return registration;
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class)
.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/login/**", "/help").permitAll()
.anyRequest().authenticated().and()
.exceptionHandling().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/")).and()
.logout().logoutSuccessUrl("/").and()
.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
}
@Bean
@ConfigurationProperties("facebook")
public ClientResources facebook() {
return new ClientResources();
}
private Filter ssoFilter() {
CompositeFilter filter = new CompositeFilter();
List<Filter> filters = new ArrayList<>();
filters.add(ssoFilter(facebook(), "/login/facebook"));
//add more authorization servers here
filter.setFilters(filters);
return filter;
}
private Filter ssoFilter(ClientResources client, String path) {
OAuth2ClientAuthenticationProcessingFilter filter = new OAuth2ClientAuthenticationProcessingFilter(path);
OAuth2RestTemplate template = new OAuth2RestTemplate(client.getClient(), oauth2ClientContext);
filter.setRestTemplate(template);
filter.setTokenServices(new UserInfoTokenServices(
client.getResource().getUserInfoUri(), client.getClient().getClientId()));
return filter;
}
class ClientResources {
@NestedConfigurationProperty
private AuthorizationCodeResourceDetails client = new AuthorizationCodeResourceDetails();
@NestedConfigurationProperty
private ResourceServerProperties resource = new ResourceServerProperties();
public AuthorizationCodeResourceDetails getClient() {
return client;
}
public ResourceServerProperties getResource() {
return resource;
}
}
}
고르 필터가 존재하는 방식으로 구현 : 다른 한편으로
facebook.client.client-id=...
facebook.client.client-secret=...
facebook.client.access-token-uri=https://graph.facebook.com/oauth/access_token
facebook.client.user-authorization-uri=https://www.facebook.com/dialog/oauth
facebook.client.token-name=oauth_token
facebook.client.authentication-scheme=query
facebook.client.client-authentication-scheme=form
facebook.resource.user-info-uri=https://graph.facebook.com/me
: 페이스 북에 관련된
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CORSFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
응용 프로그램 등록 측면 - 나는에서 호스팅되는 프레 젠 테이션 레이어 (반응 + axious 애플 리케이션)을 개발 중입니다 : , 여기서 "http://localhost:8080/login/facebook"으로 GET을 호출하고 facebook의 로그인 페이지로 리디렉션 할 의도가 있었지만 결코 발생하지 않았습니다. 대신 내가 브라우저에서 받고 있어요 :
XMLHttpRequest cannot load https://www.facebook.com/dialog/oauth?client_id=...&redirect_uri=http://localhost:8080/login/facebook&response_type=code&state=335Pc0. Redirect from 'https://www.facebook.com/dialog/oauth?client_id=...&redirect_uri=http://localhost:8080/login/facebook&response_type=code&state=335Pc0' to 'https://www.facebook.com/login.php?skip_api_login=1&api_key=..._&display=page&locale=en_US&logger_id=13caa792-a9a9-4187-bdb3-732702703d31' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
을 동시에, 봄 부트 측면에서 로그 :
[nio-8080-exec-4] o.s.s.web.DefaultRedirectStrategy : Redirecting to 'https://www.facebook.com/dialog/oauth?client_id=...&redirect_uri=http://localhost:8080/login/facebook&response_type=code&state=335Pc0'
사람이 유스 케이스를 사용하도록 설정하는 방법에 대한 조언을 할 수 있습니까?
정말이 솔루션은 복잡 관심과 대답,
비탈리