博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
6.2. EnableWebSecurity
阅读量:6433 次
发布时间:2019-06-23

本文共 4688 字,大约阅读时间需要 15 分钟。

6.2.1. 访问控制列表(Access Control List,ACL)

放行所有请求

http.authorizeRequests().antMatchers("/**" ).permitAll();
package cn.netkiller.config;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@Configuration@EnableWebSecuritypublic class WebSecurityConfig extends WebSecurityConfigurerAdapter {	public WebSecurityConfig() {		// TODO Auto-generated constructor stub	}	@Override	protected void configure(HttpSecurity http) throws Exception {		http	        .authorizeRequests()	            .antMatchers("/", "/about.html", "/doc/**").permitAll()	            .anyRequest().authenticated()	            .and()	        .formLogin()	            .loginPage("/login.html")	            .permitAll()	            .and()	        .logout()	            .permitAll();           	}		@Autowired    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {        auth            .inMemoryAuthentication()                .withUser("user").password("password").roles("USER")                .and()                .withUser("admin").password("admin").roles("ADMIN");    }}

6.2.2. WebSecurity

用于Web静态资源的权限控制

package com.example.api.config;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;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;@Configuration@EnableWebSecuritypublic class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {	@Override	public void configure(WebSecurity web) throws Exception {		web.ignoring().antMatchers("/static/**", "/**/*.jsp");	}	protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {		auth.inMemoryAuthentication().withUser("user1").password("secret1").roles("USER").and().withUser("user2").password("secret2").roles("USER").and().withUser("admin").password("secret").roles("ADMIN");	}	@Override	protected void configure(HttpSecurity http) throws Exception {		http.authorizeRequests().anyRequest().fullyAuthenticated();		http.httpBasic();		http.csrf().disable();	}}

6.2.3. HTTP Auth

@Override	protected void configure(HttpSecurity http) throws Exception {		http.authorizeRequests().antMatchers("/ping","/v1/*/ping","/v1/public/**" ).permitAll()		.anyRequest().authenticated()		.and().rememberMe().and().httpBasic()		.and().csrf().disable();	}

6.2.4. Rest

protected void configure(HttpSecurity http) throws Exception {    http      .csrf().disable()      .authorizeRequests()        .antMatchers(HttpMethod.POST, "/api/**").authenticated()        .antMatchers(HttpMethod.PUT, "/api/**").authenticated()        .antMatchers(HttpMethod.DELETE, "/api/**").authenticated()        .anyRequest().permitAll()        .and()      .httpBasic().and()      .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);}

6.2.5. hasRole

@Override    protected void configure(HttpSecurity http) throws Exception {             http.authorizeRequests()        .antMatchers("/", "/member").access("hasRole('USER') or hasRole('ADMIN') or hasRole('DBA')")        .and().formLogin().loginPage("/login")        .usernameParameter("sso").passwordParameter("password")        .and().exceptionHandling().accessDeniedPage("/403");    }

6.2.6. Add User

@Autowired    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {        auth            .inMemoryAuthentication()                .withUser("user").password("password").roles("USER");    }

添加多个用户

@Autowired    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {        auth            .inMemoryAuthentication()                .withUser("user").password("password").roles("USER")                .and()                .withUser("admin").password("admin").roles("ADMIN")                .and()                .withUser("admin").password("super").roles("ADMIN","SYS","DBA")                ;    }

原文出处:Netkiller 系列 手札

本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

你可能感兴趣的文章
php token验证范例
查看>>
WebSocket的C++服务器端实现
查看>>
java中两种添加监听器的策略
查看>>
MySQL explain 详解
查看>>
脑洞成现实!AI系统可提前10s预测地震
查看>>
Page页面生命周期——微信小程序
查看>>
Node.js编写CLI的实践
查看>>
Javascript数组对象的方法和属性
查看>>
SQL Server 2005自动异机备份
查看>>
MariaDB 10审计日志去除记录select操作
查看>>
Golang之环境配置
查看>>
03springmvc入门
查看>>
XenApp_XenDesktop_7.6实战篇之十一:站点设置
查看>>
zabbix1.8和2.0版本通用的安装脚本
查看>>
电商订单 + 物流信息对称补齐案例 - A, B表,到达时间交叉,增量JOIN补全C数据...
查看>>
vs2010新特性(下)
查看>>
WPF/E CTP Quick Start - 第四部分:绘图与填充(翻译)
查看>>
[CTO札记]架构改造(SOBS)4原则
查看>>
cocos2d-x一些核心概念介绍
查看>>
SAS硬盘驱动器走向成熟(10款SAS硬盘横评之一)
查看>>