博客
关于我
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/

你可能感兴趣的文章
pintos project (2) Project 1 Thread -Mission 1 Code
查看>>
PinYin4j库的使用
查看>>
PIP
查看>>
pip install goose-extractor // SyntaxError: Missing parentheses in call to 'print'
查看>>
pip install mysqlclient报错
查看>>
pip install 出现报asciii码错误的解决
查看>>
pip throws TypeError: parse() got an unexpected keyword argument ‘transport_encoding‘ 在尝试安装新软件包时
查看>>
pip 下载慢
查看>>
pip 升级报错AttributeError: ‘NoneType’ object has no attribute ‘bytes’
查看>>
pip 安装opencv-python卡死
查看>>
pip 安装出现异常
查看>>
Pip 安装失败:需要 SSL
查看>>
Pip 安装挂起
查看>>
pip 或 pip3 为 Python 3 安装包?
查看>>
pip 文件损坏导致 pip无法使用 报错 ImportError: cannot import name 'main' from 'pip._int
查看>>
pip 无法从 requirements.txt 安装软件包
查看>>
pip/pip3更换国内源
查看>>
pip3 install PyQt5 --user 失败
查看>>
pip3命令全解析:Python3包管理工具的详细使用指南
查看>>
pip3安装命令重复创建文件‘/tmp/pip-install-xxxxx/package‘失败
查看>>