SpringSecurity学习笔记之 入门 二 (From表单验证方式【比较少用】,跨域问题)

适合没有前后端分离的传统项目;【少见少见】
配置拦截认证规则的时候设置为form表单模式:

// 配置拦截规则,认证方式的方法
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 配置所有路径都需要验证,并且  and指定 使用 httpbasic 验证
        http.authorizeRequests().antMatchers("/**").fullyAuthenticated().and().httpBasic();
    }

改变以后我们再次未登录访问会跳转到一个security内置的from表单验证页面


配置权限

//配置权限,授予权限
http.authorizeRequests()
		.antMatchers("/add").hasAnyAuthority("add")
        .antMatchers("/update").hasAnyAuthority("update")
        .antMatchers("/del").hasAnyAuthority("del")
        .antMatchers("/**").hasAnyAuthority("admin");
        //也可以如下写
   // .antMatchers("/add","/update","del").hasAnyAuthority("admin")

拦截并验证所有请求authorizeRequests()
设置路径antMatchers("/add")设置路径的校验方案.hasAnyAuthority("add")

分别对应:
给路径绑定多个权限放行策略
给路径绑定多个角色放行策略
给路径绑定一个权限放行策略
给路径绑定一个IP放行策略
给路径绑定一个角色放行策略
整体思路,给一个路径配置好访问他需要的权限,然后再给用户配置对应的权限或者角色;不想用户访问,修改权限的授权路径和修改角色的授权都可以实现;
注意注意!!
这里是,给路径配置权限,你可以给一个路径配置多个权限
·.antMatchers("/del").hasAnyAuthority("del","admin")
持有这些权限的都可以访问;
但是如果你配置成这样
·.antMatchers("/del","update").hasAnyAuthority("admin")
希望admin可以同时访问del那是错误的!这样的意思是,给多个路径配置同样的一个权限,也就是把del和update都配置成只有拥有admin才能访问;
想要实现admin效果,需要在上面给admin用户授权的时候把多个权限授予他;
也可以在每一个路径后面都加上admin这个权限,然后把admin权限给admin用户就可以了


完整版: 删除了上面只是修改为表单的那段。加在了授权后面
 // 配置拦截规则,认证方式的方法
 @Override
 protected void configure(HttpSecurity http) throws Exception {
     //配置权限
     http.authorizeRequests().antMatchers("/add").hasAnyAuthority("add")
             .antMatchers("/update").hasAnyAuthority("update")
             .antMatchers("/del").hasAnyAuthority("del")
             .antMatchers("/**").fullyAuthenticated().and().formLogin()
     ;
 }

给用户授权(写死版,不推荐,演示用):

// 配置账户方法
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    //配置一个新账户,配置密码,并且设置一个匹配的路径
    auth.inMemoryAuthentication().withUser("add").password("123456").authorities("add");
    auth.inMemoryAuthentication().withUser("update").password("123456").authorities("update");
    auth.inMemoryAuthentication().withUser("del").password("123456").authorities("del");
    auth.inMemoryAuthentication().withUser("admin").password("123456").authorities("admin");
    auth.inMemoryAuthentication().withUser("admin2").password("123456").authorities("add","update","del");
}

在前后端分离中会产生跨域问题

我这里的情况是,我定义了一个接口。。/user/login
然后配置默认的登录请求处理接口。。/user/login,这时候程序是会检测这个路径的登录请求,并使用表单登录逻辑校验他;所以并不会访问我设置的那个接口,所及即便我在接口上配置了跨域依然无法解决跨域问题;
但是security本身映射的那个处理登录的接口并没有解决跨域问题,所以我请求永远都是跨域错误;

解决的办法就是在请求到来之前就用过滤器给他在请求头里加上跨域访问许可

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class GlobalCorsConfig {
/**
* 允许跨域调用的过滤器
*/
	@Bean
	public CorsFilter corsFilter() {
	CorsConfiguration config = new CorsConfiguration();
	//允许所有域名进行跨域调用
	config.addAllowedOrigin("*");
	//允许跨越发送cookie
	config.setAllowCredentials(true);
	//放行全部原始头信息
	config.addAllowedHeader("*");
	//允许所有请求方法跨域调用
	config.addAllowedMethod("*");
	UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
	source.registerCorsConfiguration("/**", config);
	return new CorsFilter(source);
		}
}

或者在在SecurityConfig类的configure(HttpSecurity httpSecurity)方法中添加如下代码

http.authorizeRequests().antMatchers(HttpMethod.OPTIONS)//跨域请求会先进行一次options请求
.permitAll()


网上其他解决办法【跳转】

/**
     * cors跨域
     *
     * @return object
     */
    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.setMaxAge(3600L);
        corsConfiguration.addExposedHeader("access-control-allow-methods");
        corsConfiguration.addExposedHeader("access-control-allow-headers");
        corsConfiguration.addExposedHeader("access-control-allow-origin");
        corsConfiguration.addExposedHeader("access-control-max-age");
        corsConfiguration.addExposedHeader("X-Frame-Options");

        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration(AUTH_URL_REG, corsConfiguration);
        return configurationSource;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值