繁体   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