java csrf攻击,Spring-Security对CSRF攻击的支持

何时使用CSRF保护

什么时候应该使用CSRF保护?我们的建议是使用CSRF保护,可以通过浏览器处理普通用户的任何请求。如果你只是创建一个非浏览器客户端使用的服务,你可能会想要禁用CSRF保护。(即所有处理来自浏览器的请求需要是CSRF保护,如果后台服务是提供API调用那么可能就要禁用CSRF保护)

配置CSRF保护

CSRF保护默认情况下使用Java配置启用

@EnableWebSecuritypublic class WebSecurityConfig extendsWebSecurityConfigurerAdapter {

@Overrideprotected void configure(HttpSecurity http) throwsException {

http

.csrf().disable();//禁用CSRF保护

}

}

wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

前提注意:SpringBoot实例中应用CSRF

The URL that triggers log out to occur (default is /logout). If CSRF protection is enabled (default), then the request must also be a POST. For more information, please consult the JavaDoc.

CSRF在SpringSecurity中默认是启动的,那么你的退出请求必须改为POST请求。这确保了注销需要CSRF令牌和一个恶意的用户不能强制注销用户

所以在SpringSecurity中需要重新配置登出

packagecom.niugang.config;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.annotation.Configuration;importorg.springframework.http.HttpMethod;importorg.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;importorg.springframework.security.config.annotation.web.builders.HttpSecurity;importorg.springframework.security.config.annotation.web.builders.WebSecurity;importorg.springframework.security.config.annotation.web.configuration.EnableWebSecurity;importorg.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;importorg.springframework.security.core.userdetails.UserDetailsService;importorg.springframework.security.web.util.matcher.AntPathRequestMatcher;

@Configuration//里面已经包含了@Component 所以不用再上下文中在引入入了

@EnableWebSecuritypublic class SecurityConfig extendsWebSecurityConfigurerAdapter {//spring自带的

@AutowiredprivateUserDetailsService userDetailsService;/*** configure(HttpSecurity)方法定义了哪些URL路径应该被保护*/@Overrideprotected void configure(HttpSecurity http) throwsException {

http.authorizeRequests()//该方法所返回的对象的方法来配置请求级别的安全细节

.antMatchers("/login").permitAll() //登录页面不拦截

.antMatchers("/api/**").permitAll() //调用api不需要拦截

.antMatchers(HttpMethod.POST, "/checkLogin").permitAll().anyRequest()

.authenticated()//对于登录路径不进行拦截

.and().formLogin()//配置登录页面

.loginPage("/login")//登录页面的访问路径;

.loginProcessingUrl("/checkLogin")//登录页面下表单提交的路径

.failureUrl("/login?paramserror=true")//登录失败后跳转的路径,为了给客户端提示

.defaultSuccessUrl("/index")//登录成功后默认跳转的路径;

.and().logout()//用户退出操作

.logoutRequestMatcher(new AntPathRequestMatcher("/logout","POST"))//用户退出所访问的路径,需要使用Post方式

.permitAll().logoutSuccessUrl("/login?logout=true")//退出成功所访问的路径

;

}/*** 忽略静态资源*/@Overridepublic void configure(WebSecurity web) throwsException {/** 在springboot中忽略静态文件路径,直接写静态文件的文件夹 springboot默认有静态文件的放置路径,如果应用spring

* security,配置忽略路径 不应该从springboot默认的静态文件开始

* 如:在本项目中,所有的js和css都放在static下,如果配置忽略路径,则不能以static开始

* 配置成web.ignoring().antMatchers("/static/*");这样是不起作用的*/web.ignoring().antMatchers("/themes/**", "/script/**");

}/*** 配置自定义用户服务*/@Overrideprotected void configure(AuthenticationManagerBuilder auth) throwsException {

auth.userDetailsService(userDetailsService);//.passwordEncoder(passwordEncoder());

}/*** 密码加密*/

/** @Bean public BCryptPasswordEncoder passwordEncoder() { return new

* BCryptPasswordEncoder(); }*/}

HTML中需要以表单形式POST提交退出

退出

wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

在登录页面输入用户名和密码,点击登录,页面报如下错误

c770a86e2289721b1d63d7280018a5a0.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==?

这就是页面在登录时没有向后台传入后台颁发的令牌,具体代码如下:

Insert title here

}

spring boot

姓名

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值