- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
AngularJS
index.html
<head>
<meta name="_csrf" content="${_csrf.token}"/>
<!-- default header name is X-CSRF-TOKEN -->
<meta name="_csrf_header" content="${_csrf.headerName}"/>
</head>
SpringSecurity 3.2
Spring 使用 HttpSessionCsrfTokenRepository,默认情况下将 CSRF 的 header 名称指定为 X-CSRF-TOKEN,但 Anuglar 约定为 X-XSRF-TOKEN>
我想扩展HttpSessionCsrfTokenRepository并覆盖 header 名称,但由于它被标记为final,所以我最终实现了一个自定义 token 存储库。
@Component
public class CustomCsrfTokenRepository implements CsrfTokenRepository {
public static final String CSRF_PARAMETER_NAME = "_csrf";
public static final String CSRF_HEADER_NAME = "X-XSRF-TOKEN";
private final Map<String, CsrfToken> tokenRepository = new ConcurrentHashMap<>();
public CustomCsrfTokenRepository() {
log.info("Creating {}", CustomCsrfTokenRepository.class.getSimpleName());
}
@Override
public CsrfToken generateToken(HttpServletRequest request) {
return new DefaultCsrfToken(CSRF_HEADER_NAME, CSRF_PARAMETER_NAME, createNewToken());
}
@Override
public void saveToken(CsrfToken token, HttpServletRequest request, HttpServletResponse response) {
String key = getKey(request);
if (key == null)
return;
if (token == null) {
tokenRepository.remove(key);
} else {
tokenRepository.put(key, token);
}
}
@Override
public CsrfToken loadToken(HttpServletRequest request) {
String key = getKey(request);
return key == null ? null : tokenRepository.get(key);
}
private String getKey(HttpServletRequest request) {
return request.getHeader("Authorization");
}
private String createNewToken() {
return UUID.randomUUID().toString();
}
}
SecurityConfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Inject
private CustomCsrfTokenRepository customCsrfTokenRepository;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// .addFilterAfter(new CsrfTokenGeneratorFilter(), CsrfFilter.class)
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.and()
.formLogin()
.loginProcessingUrl("/app/authentication")
.successHandler(ajaxAuthenticationSuccessHandler)
.failureHandler(ajaxAuthenticationFailureHandler)
.usernameParameter("j_username")
.passwordParameter("j_password")
.permitAll()
.and()
.csrf()
.csrfTokenRepository(customCsrfTokenRepository)
.and()
}
}
如何干净地覆盖 header 名称而不是创建自定义 csrfTokenRepository?
我还需要对单页进行其他配置更改吗AngularJS 等应用程序,因为这还无法运行。
最佳答案
当使用 Spring Security 的 Java 配置时,应该可以实现以下操作:
public void configure(final HttpSecurity http) throws Exception
{
final HttpSessionCsrfTokenRepository tokenRepository = new HttpSessionCsrfTokenRepository();
tokenRepository.setHeaderName("X-XSRF-TOKEN");
http.csrf().csrfTokenRepository(tokenRepository);
}
复杂之处在于单页应用程序依赖于 AJAX,并且在 AJAX 请求中包含 CSRF token 有点复杂。使用 AngularJS 时,服务器应在首次请求时以及用户登录或注销时发送名为 XSRF-TOKEN
的 session cookie。然后,AngularJS 将在所有请求的 HTTP header X-XSRF-TOKEN
中返回此 cookie 的值,然后服务器可以检查该值。
关于angularjs - 使用 AngularJS 和 SpringSecurity3.2 进行 CSRF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24022967/
我已经从 2.3.7 迁移到 grails 2.5.0我依赖 编译“:spring-security-core:2.0-RC5” 我将所有出现的 grails.plugins.springsecuri
考虑向 Controller 发出 GET 请求,然后 Controller 从数据库加载资源。想象数据库资源有一个标志。该标志设置资源是否是公共(public)的。如果资源标志为 true,则返回资
1.设置token的过期时间 如果我们是从数据库来读取客户端信息的话 我们只需要在数据库设置token的过期时间 1.1 oauth_client_details表每个列的作用 cli
前言 我们知道在项目开发中,后台开发权限认证是非常重要的,springboot 中常用熟悉的权限认证框架有,shiro,还有就是springboot 全家桶的 security当然他们各有各的好处,但
我有一个与 SpringSecurityCore 插件一起设置的基本 Grails 2 应用程序。这似乎工作正常。但是,当我尝试通过扩展类向我的 User.groovy 文件添加其他属性时,我似乎无法
我完成了一些教程并为自己构建了一个 Spring Security Login。但每当我登录时,我都会从日志中收到以下错误: 2015-08-12 21:39:21 DEBUG DriverManag
这是我的场景: 网络应用程序为许多应用程序执行某种 SSO 登录用户点击链接后,应用程序会向正确的应用程序发送包含用户信息(姓名、密码 [无用]、角色)的帖子 我正在其中一个应用程序上实现 Sprin
实现原理 在之前的文章中,我们介绍了普通的帐号密码登录的方式: SpringBoot + Spring Security 基本使用及个性化登录配置。 但是现在还有一种常见的方式,就是直接通过手机短
1、使用springboot+maven搭建一个多模块项目(可以参考这篇文章 --> 这里) 2、删除父工程的src文件,删除app、browser、core下的.java文
1、问题描述 在 SpringBoot 中加入 SpringSecurity 中之后,静态资源总是被过滤,导致界面很难看: 目录结构: 2、问题解决 正常不拦截资源,我查阅资料,基
我创建了两个模块 GWTAppAuth GWTApp 当我尝试从 GWTAppAuth 发布表格时至j_spring_security_check没啥事儿。 Firebug 在控制台中显示 "Fail
我是Grails和Spring Security的新手,我的目标是使用示例注册/登录/注销功能构建示例站点。注册已经准备就绪,现在我需要登录和注销。 我已经执行了s2-quickstart命令,并且具
在grails应用程序中,我成功获取了用户选择的语言(添加到url,“...?lang = xx_XX”),如下所示: def locale = RequestContextUtils.getLoca
我的successHandler deafultTargetUrl是这个 grails.plugin.springsecurity.successHandler.deafultTargetUrl =
如果他尝试访问安全资源,我如何配置 grails 不将人员/用户重定向到登录页面。我只想发送重定向到主页的 401 状态错误。 最佳答案 您要做的是配置authenticationEntryPoint
目前我正在使用以下方法将用户登录到我的应用程序。但是我想使用 Angular 函数来实际执行登录。为此,我想创建一个休息 Web 服务来进行身份验证,但是我在 SO 上看到的所有示例都使用我认为已折旧
所以我将 Spring Security 与 Spring Boot 结合使用。我想制作自己的 AuthenticationProvider,以我自己的方式使用数据库,所以我使用这个 authenti
我有一个现有的 Spring MVC 应用程序,当前未使用 SpringSecurity。我为Hibernate审计日志编写了一个AuditInterceptor,它需要一种方法来获取当前登录的用户。
我想自动登录到 Web 应用程序,该应用程序可在公司的 Intranet 上访问。 登录将以一种方式起作用,即当用户访问应用程序时,他将自动发送其凭据(用户名和密码),例如 (company.com/
很显然,@Autowired 不能在 UserDetailsService 中工作 这是真的吗?如果没有,我如何在我的 UserDetailsService 中 Autowiring ? 如果
我是一名优秀的程序员,十分优秀!