在配置文件中使用#如果在代码里配置了用户信息这个就不能使用了呢?spring.security.user.name=adminspring.security.user.password=123456spring.security.user.roles=ADMIN启动项目,localhost:8080/login进入页面,需要输入用户名和密码。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
启动项目,localhost:8080/login 进入页面,需要输入用户名和密码。用户名为:user,密码在控制台输出,去控制台查找。
在配置文件中使用
#如果在代码里配置了用户信息 这个就不能使用了呢?spring.security.user.name=adminspring.security.user.password=123456spring.security.user.roles=ADMIN
启动项目,localhost:8080/login 进入页面,需要输入用户名和密码。用户名为:admin,密码:123456
在内存中使用
- (需要将配置文件里配置的注释掉)
@Configuration@EnableWebSecurity//启用Spring security@EnableGlobalMethodSecurity(prePostEnabled = true)//拦截@preAuthrize注解的配置public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate PasswordEncoder encoder;//这个东西很重要@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {/** 基于内存的方式构建两个账户* */auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("admin").password(new BCryptPasswordEncoder().encode("123")).roles("admin");//两个构建账户的方式 看着不同 其实是一样的啊auth.inMemoryAuthentication().passwordEncoder(encoder).withUser("user").password(encoder.encode("123")).roles("normal");}}
在内存中定义认证用户,需要自己写一个类WebSecurityConfig实现WebSecurityConfigurerAdapter类,重写其中的方法;
需要注意的是:在设置密码的时候,需要是加密后的密码,且要符合加密类型;
类上面的注解 @EnableGlobalMethodSecurity开启后针对不同的方法,会验证其身份角色;
@RestControllerpublicclassHelloController{@GetMapping(value ="/hello")publicStringhello(){return"HelloWorld";}@GetMapping(value ="/helloAdmin")@PreAuthorize("hasAnyRole('admin')")publicStringhelloAdmin(){return"HelloWorld,helloAdmin";}@PreAuthorize("hasAnyRole('normal','admin')")@GetMapping(value ="/helloUser")publicStringhelloUser(){return"HelloWorld,helloUser";}}
验证:
进入localhost:8080/login,登录 admin用户,再访问:localhost:8080/helloAdmin ,localhost:8080/helloUser
均可访问成功;
重新登入 user用户,再访问localhost:8080/helloAdmin ,localhost:8080/helloUser,发现访问localhost:8080/helloAdmin时报错,访问被禁止
使用数据库1 添加依赖,使具备查询数据库的能
<!-- 数据库连接--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!--数据库--><dependency><groupId>org.hsqldb</groupId><artifactId>hsqldb</artifactId><scope>runtime</scope></dependency>
hsqldb内存数据库,jpa 连接数据库
2 开发一个根据用户名查询用户信息的接口3 编写一个类CustomUserDetailsService实现接口UserDetailsService 重写loadUserByUsername方法
publicinterfaceUserInfoService{publicUserInfofindByUsername(String username);}
@ComponentpublicclassCustomUserDetailsServiceimplementsUserDetailsService{@AutowiredprivateUserInfoService service;@AutowiredprivatePasswordEncoder encoder;@OverridepublicUserDetailsloadUserByUsername(String s)throwsUsernameNotFoundException{UserInfo userInfo = service.findByUsername(s);if(userInfo ==null) {thrownewUsernameNotFoundException("not found : "s);}List<GrantedAuthority> authorities =newArrayList<>();authorities.add(newSimpleGrantedAuthority("ROLE_"userInfo.getRole().name()));User userDetails =newUser(userInfo.getUsername(), encoder.encode(userInfo.getPassword()), authorities);returnuserDetails;}}
1 此方法返回的是一个UserDetails 实例,构造方法中有3个参数,分别为 用户名,密码,和权限列表;
2 次用用到了查询用户信息的接口
注意:此处的密码需要加密;权限需要前面拼接ROLE(权限如果提前预存在数据库已经拼接过,此处写法会不同)
4 在数据库添加用户
@ServicepublicclassDataInit{@AutowiredprivateUserInfoRepository userInfoRepository;@PostConstructpublicvoiddataInit(){UserInfo user =newUserInfo();user.setUsername("user");user.setPassword("123");user.setRole(UserInfo.Role.normal);userInfoRepository.save(user);UserInfo admin =newUserInfo();admin.setUsername("admin");admin.setPassword("123");admin.setRole(UserInfo.Role.admin);userInfoRepository.save(admin);}}
@ServicepublicclassDataInit{@AutowiredprivateUserInfoRepository userInfoRepository;@PostConstructpublicvoiddataInit(){UserInfo user =newUserInfo();user.setUsername("user");user.setPassword("123");user.setRole(UserInfo.Role.normal);userInfoRepository.save(user);UserInfo admin =newUserInfo();admin.setUsername("admin");admin.setPassword("123");admin.setRole(UserInfo.Role.admin);userInfoRepository.save(admin);}}
验证:
进入localhost:8080/login,登录 admin用户,再访问:localhost:8080/helloAdmin ,localhost:8080/helloUser
均可访问成功;
重新登入 user用户,再访问localhost:8080/helloAdmin ,localhost:8080/helloUser,发现访问localhost:8080/helloAdmin时报错,访问被禁止
源代码:https://github.com/liyiruo/bili-spring-security
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:
https://blog.csdn.net/l23456789o/article/details/107853160
,