【问题标题】:Spring Security OAuth (separate client on angular2)Spring Security OAuth(angular2上的单独客户端)
【发布时间】:2016-06-26 13:05:00
【问题描述】:

我在配置 spring security 和 oauth2 时遇到问题。 我在他们的页面上使用了一个教程,其中有一个 angular1 应用程序在同一端口上运行并由 Tomcat 提供服务。

我想以不同的方式来做。我想做的是放置一个完全独立的 angular2 应用程序,在不同的端口上运行。

现在的问题是应用只返回8080端口(spring应用),我不知道如何改变这种行为。

我的整个 Java 代码是:

@SpringBootApplication
@EnableOAuth2Sso
@RestController
public class SocialApplication extends WebSecurityConfigurerAdapter {


@RequestMapping("/user")
public Principal user(Principal principal) {
    return principal;
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .antMatcher("/**")
            .authorizeRequests()
            .antMatchers("/", "/log**", "/login**", "/webjars/**")
            .permitAll()
            .anyRequest()
            .authenticated()
            .and().logout().logoutSuccessUrl("/").permitAll()
            .and().csrf().csrfTokenRepository(csrfTokenRepository())
            .and().addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);
}

private Filter csrfHeaderFilter() {
    return new OncePerRequestFilter() {
        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                                        FilterChain filterChain) throws ServletException, IOException {
            CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
            if (csrf != null) {
                Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
                String token = csrf.getToken();
                if (cookie == null || token != null && !token.equals(cookie.getValue())) {
                    cookie = new Cookie("XSRF-TOKEN", token);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                }
            }
            filterChain.doFilter(request, response);

        }
    };
}

private CsrfTokenRepository csrfTokenRepository() {
    HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
    repository.setHeaderName("X-XSRF-TOKEN");
    return repository;
}

public static void main(String[] args) {
    SpringApplication.run(SocialApplication.class, args);
}
}

【问题讨论】:

标签: java spring oauth spring-security angular


【解决方案1】:

解决方案在这里我已经创建了一个教程。 link to tutorial is here

【讨论】:

    猜你喜欢
    • 2018-11-15
    • 2020-04-13
    • 2014-05-04
    • 1970-01-01
    • 2012-10-22
    • 2013-06-20
    • 1970-01-01
    • 2015-01-07
    • 2021-05-31
    相关资源
    最近更新 更多