博客
关于我
SpringSecurity
阅读量:293 次
发布时间:2019-03-01

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

SpringSecurity入坑第一步:内存权限验证与授权

项目依赖管理

在开始配置权限验证之前,我们需要先构建一个完整的项目依赖关系。以下是基于Spring Boot和Spring Cloud Hoxton.RC1版本的完整依赖管理配置:

4.0.0
com.shaojie.authority
authority
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.2.0.RELEASE
1.8
${java.version}
${java.version}
UTF-8
UTF-8
Hoxton.RC1
org.springframework.cloud
spring-cloud-dependencies
Hoxton.RC1
pom
import
org.project.lombok
lombok
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
com.alibaba
druid
1.1.21
org.springframework.boot
spring-boot-maven-plugin

权限验证配置

接下来,我们来看Spring Security的核心配置。权限验证是Spring Security的关键部分,我们需要在项目中添加必要的Bean配置和权限规则。

package com.shaojie.authority.security;import org.springframework.context.annotation.Bean;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;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;@Configuration@EnableWebSecuritypublic class SpringSecurityConfig extends WebSecurityConfigurerAdapter {    @Bean    public BCryptPasswordEncoder passwordEncoder() {        return new BCryptPasswordEncoder();    }    @Override    protected void configure(AuthenticationManagerBuilder auth) throws Exception {        auth.inMemoryAuthentication()                .passwordEncoder(passwordEncoder())                .withUser("shaojie")                .password(passwordEncoder().encode("123456"))                .authorities("PRODUCT_ADD", "PRODUCT_LIST");    }    @Override    protected void configure(HttpSecurity http) throws Exception {        http.authorizeRequests()                .antMatchers("/product/add").hasAnyAuthority("PRODUCT_ADD")                .antMatchers("/product/update").hasAnyAuthority("PRODUCT_UPDATE")                .antMatchers("/product/list").hasAnyAuthority("PRODUCT_LIST")                .antMatchers("/product/delete").hasAnyAuthority("PRODUCT_DELETE")                .antMatchers("/login").permitAll()                .antMatchers("/**")                .fullyAuthenticated()                .and()                .formLogin()                .loginPage("/login")                .and()                .rememberMe()                .and()                .logout()                .and()                .csrf().disable();    }}

错误页面配置

为了让用户在权限不足时有友好的提示页面,我们可以配置403错误页面。

package com.shaojie.authority.security;import org.springframework.boot.web.server.ConfigurableWebServerFactory;import org.springframework.boot.web.server.ErrorPage;import org.springframework.boot.web.server.WebServerFactoryCustomizer;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.http.HttpStatus;@Configurationpublic class ErrorPageConfig {    @Bean    public WebServerFactoryCustomizer
webServerFactoryCustomizer() { return new WebServerFactoryCustomizer
() { @Override public void customize(ConfigurableWebServerFactory factory) { factory.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/403")); } }; }}

登录与权限页面

登录页面 (login.html)

    
Login Page

Login Page

Username:
Password:

权限相关页面

  • 添加页面 (add.html)
    
Add Product

Add Product

  • 删除页面 (delete.html)
    
Delete Product

Delete Product

  • 查询页面 (list.html)
    
Query Products

Query Products

  • 修改页面 (update.html)
    
Update Product

Update Product

注意事项

  • 密码编码:在Spring Security中,默认的密码编码是使用BCryptPasswordEncoder进行加密存储。记得在配置中使用passwordEncoder()进行密码编码。
  • 权限控制:使用hasAnyAuthority方法来设置权限,支持多个权限的结合。
  • 权限验证:在配置中设置了内存用户和密码,建议在实际项目中使用数据库存储用户信息。
  • 错误页面配置:通过WebServerFactoryCustomizer实现了403错误页面的配置,建议根据实际需求添加更多错误页面。

总结

通过以上配置,我们已经完成了Spring Security的基本权限验证配置。从依赖管理到权限验证、错误页面配置等各个方面都做了相应的设置。如果需要更详细的配置或功能扩展,可以参考Spring Security的官方文档或相关示例。

转载地址:http://xkvo.baihongyu.com/

你可能感兴趣的文章
overlay(VLAN,VxLAN)、underlay网络、大二层概述
查看>>
OWASP漏洞原理<最基础的数据库 第二课>
查看>>
OWL本体语言
查看>>
P with Spacy:自定义文本分类管道
查看>>
P1035 I need help
查看>>
P1364 医院设置
查看>>
P2260 [清华集训2012]模积和
查看>>
SpringBoot中集成influxdb-java实现连接并操作Windows上安装配置的influxDB(时序数据库)
查看>>
SpringBoot中集成eclipse.paho.client.mqttv3实现mqtt客户端并支持断线重连、线程池高并发改造、存储入库mqsql和redis示例业务流程,附资源下载
查看>>
Padding
查看>>
paddlehub安装及对口罩检测
查看>>
SpringBoot中集成Actuator实现监控系统运行状态
查看>>
paddle的两阶段基础算法基础
查看>>
Page Object模式:为什么它是Web自动化测试的必备工具
查看>>
SpringBoot中重写addCorsMapping解决跨域以及提示list them explicitly or consider using “allowedOriginPatterns“ in
查看>>
PageHelper 解析及实现原理
查看>>
pageHelper分页工具的使用
查看>>
pageHelper分页技术
查看>>
PageHelper分页查询遇到的小问题
查看>>
PageHelper实现分页详细版、整合SSM应用
查看>>