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

你可能感兴趣的文章
Objective-C实现max subarray sum最大子数组和算法(附完整源码)
查看>>
Objective-C实现max sum sliding window最大和滑动窗口算法(附完整源码)
查看>>
Objective-C实现MaxHeap最大堆算法(附完整源码)
查看>>
Objective-C实现MaximumSubarray最大子阵列(Brute Force蛮力解决方案)算法(附完整源码)
查看>>
Objective-C实现MaximumSubarray最大子阵列(动态规划解决方案)算法(附完整源码)
查看>>
Objective-C实现maxpooling计算(附完整源码)
查看>>
Objective-C实现max_difference_pair最大差异对算法(附完整源码)
查看>>
Objective-C实现max_heap最大堆算法(附完整源码)
查看>>
Objective-C实现MD5 (附完整源码)
查看>>
Objective-C实现md5算法(附完整源码)
查看>>
Objective-C实现MeanSquareError均方误差算法 (附完整源码)
查看>>
Objective-C实现median filter中值滤波器算法(附完整源码)
查看>>
Objective-C实现memcmp函数功能(附完整源码)
查看>>
Objective-C实现memcpy函数功能(附完整源码)
查看>>
Objective-C实现memoization优化技术算法(附完整源码)
查看>>
Objective-C实现memset函数功能(附完整源码)
查看>>
Objective-C实现merge insertion sort合并插入排序算法(附完整源码)
查看>>
Objective-C实现merge sort归并排序算法(附完整源码)
查看>>
Objective-C实现mergesort归并排序算法(附完整源码)
查看>>
Objective-C实现MidpointIntegration中点积分算法 (附完整源码)
查看>>