java中登录失败的弹出框_java – Spring Boot安全性在登录失败后显示Http-Basic-Auth弹出窗口...

我目前正在为学校项目,Spring Boot后端和AngularJS前端创建一个简单的应用程序,但是我似乎无法解决安全问题.

登录工作完美,但是当我输入错误的密码时,默认的登录弹出窗口显示出来,这有点烦人.我已经尝试了注释’BasicWebSecurity’并将httpBassic置于禁用状态,但没有结果(意味着登录过程根本不起作用).

我的安全类:

package be.italent.security;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.autoconfigure.security.SecurityProperties;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.annotation.Order;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;

import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;

import org.springframework.security.config.annotation.web.builders.WebSecurity;

import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import org.springframework.security.core.userdetails.UserDetailsService;

import org.springframework.security.web.csrf.CsrfFilter;

import org.springframework.security.web.csrf.CsrfToken;

import org.springframework.security.web.csrf.CsrfTokenRepository;

import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;

import org.springframework.web.filter.OncePerRequestFilter;

import org.springframework.web.util.WebUtils;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.ServletException;

import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

@Configuration

@EnableGlobalMethodSecurity(prePostEnabled = true)

@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired

private UserDetailsService userDetailsService;

@Autowired

public void configure(AuthenticationManagerBuilder auth) throws Exception {

auth.userDetailsService(userDetailsService);

}

@Override

public void configure(WebSecurity web){

web.ignoring()

.antMatchers("/scripts/**/*.{js,html}")

.antMatchers("/views/about.html")

.antMatchers("/views/detail.html")

.antMatchers("/views/home.html")

.antMatchers("/views/login.html")

.antMatchers("/bower_components/**")

.antMatchers("/resources/*.json");

}

@Override

protected void configure(HttpSecurity http) throws Exception {

http.httpBasic()

.and()

.authorizeRequests()

.antMatchers("/user","/index.html","/","/projects/listHome","/projects/{id}","/categories","/login").permitAll().anyRequest()

.authenticated()

.and()

.csrf().csrfTokenRepository(csrfTokenRepository())

.and()

.addFilterAfter(csrfHeaderFilter(),CsrfFilter.class).formLogin();

}

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;

}

}

有没有人知道如何防止这个弹出窗口显示而不打破其余部分?

将此添加到我的Angular配置中:

myAngularApp.config(['$httpProvider',function ($httpProvider) {

$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

}

]);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值