介绍

这里学习SpringSecurity,对SpringSecurity进行学习。

基本用法

添加依赖

  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-security</artifactId>

  4. </dependency>

添加接口

  1. package com.example.demo.web;

  2.  

  3. import org.springframework.web.bind.annotation.RequestMapping;

  4. import org.springframework.web.bind.annotation.RestController;

  5.  

  6. @RestController

  7. @RequestMapping("/test")

  8. public class Test {

  9. @RequestMapping("/test")

  10. public String test(){

  11. return "test";

  12. }

  13. }

启动项目

可以看到日志中,已经有了密码

Spring Security 第一季_Spring Security

访问接口,此时已经有了登录页面

Spring Security 第一季_Spring Security _02

输入用户名和密码

  1. 用户名: user

  2. 密码 984cccf2-ba82-468e-a404-7d32123d0f9c

Spring Security 第一季_Spring Security _03

此时已经登录成功

配置用户名和密码

在配置文件中,进行配置

  1. spring:

  2. security:

  3. user:

  4. name: ming

  5. password: 123456

  6. roles: admin

` 输入用户名和密码,可以正常登录

基于内存的认证

需要自定义类继承 WebSecurityConfigurerAdapter 实现自定义的配置 这里基于内存的配置,如下

  1. package com.example.demo.config;

  2.  

  3. import org.springframework.context.annotation.Bean;

  4. import org.springframework.context.annotation.Configuration;

  5. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;

  6. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

  7. import org.springframework.security.crypto.password.NoOpPasswordEncoder;

  8. import org.springframework.security.crypto.password.PasswordEncoder;

  9.  

  10.  

  11. @Configuration

  12. public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {

  13. @Bean

  14. PasswordEncoder passwordEncoder(){

  15. return NoOpPasswordEncoder.getInstance();

  16. }

  17.  

  18. @Override

  19. protected void configure(AuthenticationManagerBuilder auth) throws Exception {

  20. auth.inMemoryAuthentication()

  21. .withUser("admin").password("123").roles("admin");

  22. }

  23. }

这里基于内存的配置

HttpSecurity

这里对某些方法进行拦截

  1. package com.ming.demo.interceptor;

  2.  

  3. import org.springframework.beans.factory.annotation.Autowired;

  4. import org.springframework.context.annotation.Bean;

  5. import org.springframework.context.annotation.Configuration;

  6. import org.springframework.http.HttpMethod;

  7. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;

  8. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;

  9. import org.springframework.security.config.annotation.web.builders.HttpSecurity;

  10. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

  11. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

  12. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

  13. import org.springframework.security.crypto.password.PasswordEncoder;

  14. import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices;

  15.  

  16. @Configuration

  17. @EnableWebSecurity

  18. public class SecurityConfig extends WebSecurityConfigurerAdapter {

  19. //基于内存的用户存储

  20. @Override

  21. public void configure(AuthenticationManagerBuilder auth) throws Exception {

  22. auth.inMemoryAuthentication()

  23. .withUser("itguang").password("123456").roles("USER").and()

  24. .withUser("admin").password("{noop}" + "123456").roles("ADMIN");

  25. }

  26.  

  27.  

  28.  

  29.  

  30.  

  31. //请求拦截

  32. @Override

  33. protected void configure(HttpSecurity http) throws Exception {

  34. http.authorizeRequests()

  35. .anyRequest().permitAll()

  36. .and()

  37. .formLogin()

  38. .permitAll()

  39. .and()

  40. .logout()

  41. .permitAll();

  42. }

  43.  

  44.  

  45. }

`

这里成功完成了post请求进行登录验证。

Spring Security 第一季_Spring Security _04

小明菜市场