我正在开发一个Spring Boot应用程序,我在其中定义了要执行的过滤器,以管理令牌的获取和验证。因此,在我的Web安全类配置中,我设法做到了这一点:
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
// we don't need CSRF because our token is invulnerable
.csrf().disable()
// don't create session
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests()
// .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
// allow anonymous resource requests
.antMatchers(HttpMethod.GET, "/", "/*.html", "/favicon.ico", "/**/*.html", "/**/*.css", "/**/*.js").permitAll()
.antMatchers("/auth/**").permitAll()
.antMatchers("/places/public").permitAll()
.anyRequest().authenticated();
// Custom JWT based security filter
httpSecurity.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);
// disable page caching
httpSecurity.headers().cacheControl();
}
public class JwtFilter extends OncePerRequestFilter{
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private JwtService jwtService;
@Value("${jwt.header}")
private String authorizationHeader;
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
String token = httpServletRequest.getHeader(authorizationHeader);
String username = jwtService.getUsernameFromToken(token);
if(username!=null && SecurityContextHolder.getContext().getAuthentication() ==null){
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
if(jwtService.isTokenValid(token, userDetails)){
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}
}
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
}
更改您的WebSecurityConfiguration类,它将如下所示:
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
// we don't need CSRF because our token is invulnerable
.csrf().disable()
// don't create session
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests()
.anyRequest().authenticated();
// Custom JWT based security filter
httpSecurity.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);
// disable page caching
httpSecurity.headers().cacheControl();
}
@Override
public void configure(WebSecurity web) throws Exception {
// Ignore spring security in these paths
web.ignoring().antMatchers("/", "/*.html", "/favicon.ico", "/**/*.html", "/**/*.css", "/**/*.js","/auth/**","/places/public");
}
您必须从HttpSecurity configure方法中删除permitAll匹配器,并将其放入WebSecurity configure方法中。
我从服务器得到一个200的响应,但是access-control-allog-origin:null。 所以Chrome正在抱怨上面的错误。 尝试在控制器中手动设置标题,但现在我可以看到两个标题具有相同的名称。Chrome再次抱怨多个标题同名。
问题内容: 我看到以下错误: 使用此代码: 是什么原因引起的,如何解决? 问题答案: 在当前域之外发出ajax请求时,Javascript是受限制的。 例1:您的域名为example.com,并且您想向test.com提出请求=>您不能。 例2:您的域名是example.com,并且您想向inner.example.com发送请求,但是您不能。 例3:您的域名为example.com:80,并且您
我在AngularJS中创建了一个应用程序,并尝试调用Laravel API: MyApp(AngularJS):http://localhost:8080/ API(Laravel样板文件):http://localhost:8000/ api_routes.php:
我正在使用Microsoft扩展访问Azure DevOps中的密钥库,从密钥库中获取秘密。我得到了这个错误消息,它似乎说我需要允许Azure Devops代理访问KeyVault的权限。“无法获取托管服务主体的访问令牌。请为虚拟机”https://aka.ms/azure-MSI-docs“配置托管服务标识(MSI)。状态代码:400,状态消息:错误请求” 我在一个单独的测试订阅中运行这个功能,
我在从node.js服务器获取数据时遇到问题。 客户端是: 在服务器端,我还设置了标题: 但我犯了个错误 "XMLHttpRequest无法加载http://localhost:3003/get_testlines.对预试请求的响应没有通过权限改造检查:请求的资源上没有'Access-Control-Prover-Origin'标头。因此,来源http://localhost:3000不允许进入。
我正在使用Spring Boot和Spring Security。我有多个URL,我希望其中一些可以通过身份验证访问,而其中一些可以在没有身份验证的情况下访问。我以这种方式编写了Configure方法。但浏览器会将我重定向到登录页面,即使是允许在没有身份验证的情况下访问的URL。例如,当我尝试访问localhost:8080/emailExists时,我被要求登录。我会做错什么? 我的控制器类: