简体   繁体   English

Spring Security OAuth(angular2上的单独客户端)

[英]Spring Security OAuth (separate client on angular2)

I have a problem with configuring spring security and oauth2. 我在配置spring security和oauth2时遇到问题。 I used a tutorial on their page, where there was an angular1 app that was running on the same port and was served from Tomcat. 我在他们的页面上使用了一个教程,其中有一个angular1应用程序在同一个端口上运行,并且是从Tomcat提供的。

I want to do it in a different way. 我想以不同的方式做到这一点。 What I want to do is put a completely separate angular2 app, running on a different port. 我想要做的是在一个不同的端口上运行一个完全独立的angular2应用程序。

Now the problem is that the app only returns to port 8080 (spring app) and I don't know how to change this behavior. 现在问题是app只返回端口8080(spring app),我不知道如何改变这种行为。

My whole Java code is: 我的整个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);
}
}

Solution is here I have create a tutorial. 解决方案在这里我已经创建了一个教程。 link to tutorial is here 链接到教程就在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM