Spring Security官网:https://spring.io/projects/spring-security#learn.
一、Spring Security简述Spring Security 是一个功能强大且高度可定制的身份验证和访问控制框架。它是保护基于 Spring 的应用程序的事实上的标准。
Spring Security 是一个专注于为 Java 应用程序提供身份验证和授权的框架。与所有 Spring 项目一样,Spring Security 的真正强大之处在于它可以轻松扩展以满足自定义要求。
二、使用Spring Security准备一个数据库
准备application.properties
#关闭缓存 spring.thymeleaf.cache=false server.port=8081 #数据库配置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/security?characterEncoding=UTF-8&useSSL=false spring.datasource.username=root spring.datasource.password=123456
准备pom.xml文件
4.0.0 org.springframework.boot spring-boot-starter-parent2.0.9.RELEASE com.cao springsecurity0.0.1-SNAPSHOT springsecurity Demo project for Spring Boot 1.8 org.mybatis.spring.boot mybatis-spring-boot-starter2.1.3 mysql mysql-connector-java5.1.47 org.projectlombok lombokorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-starter-thymeleaforg.springframework.boot spring-boot-starter-securityorg.thymeleaf.extras thymeleaf-extras-springsecurity53.0.4.RELEASE org.springframework.boot spring-boot-maven-pluginsrc/main/java ** UserDetails loadUserByUsername(String username) throws UsernameNotFoundException; }
AuthorityUtils工具类作用,可在源码中查看
* Creates a array of GrantedAuthority objects from a comma-separated string * representation (e.g. "ROLE_A, ROLE_B, ROLE_C"). * 创建一个被授予权限的数组,由逗号分隔的字符串表示 * (例如:“ROLE_A,ROLE_B,ROLE_C”)。
Controller
package com.jsxl.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class RouterController { @RequestMapping({"/", "/index"}) public String index() { return "index"; } @RequestMapping("/toLogin") public String toLogin() { return "views/login"; } @RequestMapping("/level1/{id}") public String level1(@PathVariable("id") int id) { return "views/level1/" + id; } @RequestMapping("/level2/{id}") public String level2(@PathVariable("id") int id) { return "views/level2/" + id; } @RequestMapping("/level3/{id}") public String level3(@PathVariable("id") int id) { return "views/level3/" + id; } }
SecurityConfig
package com.jsxl.config; import org.springframework.beans.factory.annotation.Autowired; 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.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Override protected void configure(HttpSecurity http) throws Exception { //首页所有人可以访问,功能页有相应权限才能访问 //链式编程 //请求授权的规则 http.authorizeRequests() .antMatchers("/","/toLogin").permitAll() .antMatchers("/level1 protected void configure(AuthenticationManagerBuilder auth) throws Exception { this.disableLocalConfigureAuthenticationBldr = true; }
spring boot启动类
package com.jsxl; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.jsxl.mapper") public class SpringsecurityApplication { public static void main(String[] args) { SpringApplication.run(SpringsecurityApplication.class, args); } }
测试结果
git代码下载:https://gitee.com/jsxlliar/spring_security.git.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)