簡體   English   中英

angularjs中的Spring Security返回禁止登錄的403

[英]Spring security in angularjs returns 403 forbidden for login

在我參與的一個新的大型應用程序中,我正在嘗試使用spring-security。
我使用了本指南 ,因為它模仿了我想做的事情:

盡管我似乎無法登錄,但是由於/ login被禁止,並且我已經按照指南中的說明進行了操作,但是沒有運氣。

錯誤要求 錯誤要求圖片

安全配置(Java)

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .httpBasic()
                .and()
                .authorizeRequests()
                .antMatchers("/img/**", "/index.html", "/home.html", "/modules/**/**/**", "/", "/translations.js")
                .permitAll()
                .anyRequest()
                .authenticated()
                .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;
    }
}

似乎以下網址匹配模式中缺少/ login,這將跳過對列出網址的身份驗證。

.antMatchers(“ / img / ”,“ /index.html”,“ /home.html”,“ / modules / / / ”,“ /”,“ /translations.js”)

看看偉大的項目jhipster。

另外,您還將在生成的項目上找到一個示例: https : //github.com/jhipster/jhipster-sample-app/

當登錄失敗時我想返回未經授權的響應時,我遇到了相同的情況。 我通過添加一個失敗處理程序中的日志來處理它:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.formLogin().failureHandler(new AuthenticationFailureHandler() {
        private final Log logger = LogFactory.getLog();

        @Override
        public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception)
                throws IOException, ServletException {
            logger.debug("Returning UNAUTHORIZED HttpStatus: " + exception.getMessage());
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authentication Failed: " + exception.getMessage());
        }
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM