From 046cfa2c7fc2b3cff96d63502eb1d997fd46045d Mon Sep 17 00:00:00 2001 From: Bunny <1319900154@qq.com> Date: Tue, 8 Jul 2025 17:15:03 +0800 Subject: [PATCH] :tada: init spring-security --- .../SerialPort/Base-2-Window/MainWindow.xaml | 10 +- .../Base-2-Window/MainWindow.xaml.cs | 19 +- .../Base-2-Window/Model/SerialPortModel.cs | 8 +- .../ViewModel/MainWindowViewModel.cs | 22 +- .../ViewModel/OpenSerialPortCommand.cs | 25 + CSharp/WPFTutorial/WPFTutorial.sln | 2 + spring-security/pom.xml | 139 +++++ .../com/spring/SpringSecurityApplication.java | 11 + .../spring/config/DefaultSecurityConfig.java | 33 ++ .../java/com/spring/config/Knife4jConfig.java | 44 ++ .../src/main/resources/application.yml | 12 + spring-security/src/main/resources/banner.txt | 11 + .../src/main/resources/static/css/css2.css | 108 ++++ .../src/main/resources/static/error/400.html | 69 +++ .../src/main/resources/static/error/403.html | 69 +++ .../src/main/resources/static/error/404.html | 78 +++ .../src/main/resources/static/error/500.html | 64 +++ .../src/main/resources/static/error/501.html | 66 +++ .../src/main/resources/static/error/502.html | 80 +++ .../src/main/resources/static/error/503.html | 69 +++ .../src/main/resources/static/error/504.html | 81 +++ .../src/main/resources/static/error/505.html | 72 +++ .../src/main/resources/static/error/506.html | 66 +++ .../src/main/resources/static/error/507.html | 66 +++ .../src/main/resources/static/error/509.html | 66 +++ .../src/main/resources/static/error/510.html | 66 +++ .../src/main/resources/static/favicon.ico | Bin 0 -> 67758 bytes .../src/main/resources/templates/index.html | 502 ++++++++++++++++++ .../SpringSecurityApplicationTests.java | 13 + .../context/SecurityContextHolderTest.java | 24 + .../com/spring/password/PasswordTest.java | 54 ++ 31 files changed, 1924 insertions(+), 25 deletions(-) create mode 100644 CSharp/SerialPort/Base-2-Window/ViewModel/OpenSerialPortCommand.cs create mode 100644 spring-security/pom.xml create mode 100644 spring-security/src/main/java/com/spring/SpringSecurityApplication.java create mode 100644 spring-security/src/main/java/com/spring/config/DefaultSecurityConfig.java create mode 100644 spring-security/src/main/java/com/spring/config/Knife4jConfig.java create mode 100644 spring-security/src/main/resources/application.yml create mode 100644 spring-security/src/main/resources/banner.txt create mode 100644 spring-security/src/main/resources/static/css/css2.css create mode 100644 spring-security/src/main/resources/static/error/400.html create mode 100644 spring-security/src/main/resources/static/error/403.html create mode 100644 spring-security/src/main/resources/static/error/404.html create mode 100644 spring-security/src/main/resources/static/error/500.html create mode 100644 spring-security/src/main/resources/static/error/501.html create mode 100644 spring-security/src/main/resources/static/error/502.html create mode 100644 spring-security/src/main/resources/static/error/503.html create mode 100644 spring-security/src/main/resources/static/error/504.html create mode 100644 spring-security/src/main/resources/static/error/505.html create mode 100644 spring-security/src/main/resources/static/error/506.html create mode 100644 spring-security/src/main/resources/static/error/507.html create mode 100644 spring-security/src/main/resources/static/error/509.html create mode 100644 spring-security/src/main/resources/static/error/510.html create mode 100644 spring-security/src/main/resources/static/favicon.ico create mode 100644 spring-security/src/main/resources/templates/index.html create mode 100644 spring-security/src/test/java/com/spring/SpringSecurityApplicationTests.java create mode 100644 spring-security/src/test/java/com/spring/context/SecurityContextHolderTest.java create mode 100644 spring-security/src/test/java/com/spring/password/PasswordTest.java diff --git a/CSharp/SerialPort/Base-2-Window/MainWindow.xaml b/CSharp/SerialPort/Base-2-Window/MainWindow.xaml index c369293..2c22844 100644 --- a/CSharp/SerialPort/Base-2-Window/MainWindow.xaml +++ b/CSharp/SerialPort/Base-2-Window/MainWindow.xaml @@ -14,27 +14,27 @@ - + - + - + - + - + diff --git a/CSharp/SerialPort/Base-2-Window/MainWindow.xaml.cs b/CSharp/SerialPort/Base-2-Window/MainWindow.xaml.cs index 7b7d245..4087916 100644 --- a/CSharp/SerialPort/Base-2-Window/MainWindow.xaml.cs +++ b/CSharp/SerialPort/Base-2-Window/MainWindow.xaml.cs @@ -1,5 +1,6 @@ using System.IO.Ports; using System.Windows; +using Base_2_Window.Model; using Base_2_Window.ViewModel; namespace Base_2_Window; @@ -13,14 +14,14 @@ public partial class MainWindow : Window { InitializeComponent(); - DataContext = new MainWindowViewModel - { - SerialPortNames = SerialPort.GetPortNames().Select(s => s).ToList(), - BaudRates = new List { 9600 }, - DataBits = new List { 6, 7, 8 }, - StopBitsList = new List { StopBits.None, StopBits.One, StopBits.Two, StopBits.OnePointFive }, - ParityList = new List - { Parity.None, Parity.Even, Parity.Mark, Parity.None, Parity.Odd, Parity.Space } - }; + DataContext = new MainWindowViewModel(); + Command = new OpenSerialPortCommand(OpenPort); + } + + public OpenSerialPortCommand Command { get; set; } + + public void OpenPort() + { + } } \ No newline at end of file diff --git a/CSharp/SerialPort/Base-2-Window/Model/SerialPortModel.cs b/CSharp/SerialPort/Base-2-Window/Model/SerialPortModel.cs index 57f79e2..724c1b9 100644 --- a/CSharp/SerialPort/Base-2-Window/Model/SerialPortModel.cs +++ b/CSharp/SerialPort/Base-2-Window/Model/SerialPortModel.cs @@ -6,9 +6,11 @@ public class SerialPortModel { public List? SerialPortNames { get; set; } - public int? BaudRate { get; set; } + public List? BaudRates { get; set; } - public int? DataBits { get; set; } + public List? StopBitsList { get; set; } - public StopBits? StopBits { get; set; } + public List? ParityList { get; set; } + + public List? DataBits { get; set; } } \ No newline at end of file diff --git a/CSharp/SerialPort/Base-2-Window/ViewModel/MainWindowViewModel.cs b/CSharp/SerialPort/Base-2-Window/ViewModel/MainWindowViewModel.cs index 38e11dd..46bf2ac 100644 --- a/CSharp/SerialPort/Base-2-Window/ViewModel/MainWindowViewModel.cs +++ b/CSharp/SerialPort/Base-2-Window/ViewModel/MainWindowViewModel.cs @@ -1,17 +1,23 @@ using System.IO.Ports; +using Base_2_Window.Model; using Base_2_Window.MVVM; namespace Base_2_Window.ViewModel; public class MainWindowViewModel : ViewModelBase { - public List? SerialPortNames { get; set; } + public MainWindowViewModel() + { + SerialPortModel = new SerialPortModel() + { + SerialPortNames = SerialPort.GetPortNames().Select(s => s).ToList(), + BaudRates = new List { 9600 }, + DataBits = new List { 6, 7, 8 }, + StopBitsList = new List { StopBits.None, StopBits.One, StopBits.Two, StopBits.OnePointFive }, + ParityList = new List + { Parity.None, Parity.Even, Parity.Mark, Parity.None, Parity.Odd, Parity.Space } + };; + } - public List? BaudRates { get; set; } - - public List? StopBitsList { get; set; } - - public List? ParityList { get; set; } - - public List? DataBits { get; set; } + public SerialPortModel SerialPortModel { get; set; } } \ No newline at end of file diff --git a/CSharp/SerialPort/Base-2-Window/ViewModel/OpenSerialPortCommand.cs b/CSharp/SerialPort/Base-2-Window/ViewModel/OpenSerialPortCommand.cs new file mode 100644 index 0000000..bcdddf3 --- /dev/null +++ b/CSharp/SerialPort/Base-2-Window/ViewModel/OpenSerialPortCommand.cs @@ -0,0 +1,25 @@ +using System.Windows.Input; + +namespace Base_2_Window.ViewModel; + +public class OpenSerialPortCommand:ICommand +{ + public OpenSerialPortCommand(Action execute) + { + _execute = execute; + } + + private readonly Action _execute; + + public bool CanExecute(object? parameter) + { + return true; + } + + public void Execute(object? parameter) + { + _execute(); + } + + public event EventHandler? CanExecuteChanged; +} \ No newline at end of file diff --git a/CSharp/WPFTutorial/WPFTutorial.sln b/CSharp/WPFTutorial/WPFTutorial.sln index 7743eaf..01af1d3 100644 --- a/CSharp/WPFTutorial/WPFTutorial.sln +++ b/CSharp/WPFTutorial/WPFTutorial.sln @@ -139,6 +139,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo-TODO", "Demo-TODO\Demo EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASP-Demo-TODO", "ASP-Demo-TODO\ASP-Demo-TODO.csproj", "{5C964709-FAFB-4674-A59F-564D8F704D93}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WPFReview", "WPFReview", "{1AD7076E-2F79-45A6-907E-B9FA503C5DE3}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/spring-security/pom.xml b/spring-security/pom.xml new file mode 100644 index 0000000..9e370ca --- /dev/null +++ b/spring-security/pom.xml @@ -0,0 +1,139 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.5.3 + + + com.mall + spring-security + 0.0.1-SNAPSHOT + spring-security + spring-security + + + + + + + + + + + + + + + UTF-8 + 17 + 17 + 17 + 4.5.0 + 5.15.4 + 5.1.3 + 3.6.0 + + + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.security + spring-security-test + test + + + org.springframework.boot + spring-boot-devtools + runtime + + + + com.alibaba.fastjson2 + fastjson2 + 2.0.47 + + + + + com.github.xiaoymin + knife4j-openapi3-jakarta-spring-boot-starter + ${knife4j.version} + + + + + org.webjars + bootstrap + ${bootstrap.version} + + + org.webjars + font-awesome + ${font-awesome.version} + + + org.webjars + jquery + ${jquery.version} + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.thymeleaf.extras + thymeleaf-extras-springsecurity6 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + org.projectlombok + lombok + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/spring-security/src/main/java/com/spring/SpringSecurityApplication.java b/spring-security/src/main/java/com/spring/SpringSecurityApplication.java new file mode 100644 index 0000000..7cb2a3c --- /dev/null +++ b/spring-security/src/main/java/com/spring/SpringSecurityApplication.java @@ -0,0 +1,11 @@ +package com.spring; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringSecurityApplication { + public static void main(String[] args) { + SpringApplication.run(SpringSecurityApplication.class, args); + } +} diff --git a/spring-security/src/main/java/com/spring/config/DefaultSecurityConfig.java b/spring-security/src/main/java/com/spring/config/DefaultSecurityConfig.java new file mode 100644 index 0000000..083c039 --- /dev/null +++ b/spring-security/src/main/java/com/spring/config/DefaultSecurityConfig.java @@ -0,0 +1,33 @@ +package com.spring.config; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.AuthenticationEventPublisher; +import org.springframework.security.authentication.DefaultAuthenticationEventPublisher; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.crypto.factory.PasswordEncoderFactories; +import org.springframework.security.provisioning.InMemoryUserDetailsManager; + +@EnableWebSecurity +@Configuration +public class DefaultSecurityConfig { + @Bean + @ConditionalOnMissingBean(UserDetailsService.class) + InMemoryUserDetailsManager inMemoryUserDetailsManager() { + String generatedPassword = PasswordEncoderFactories.createDelegatingPasswordEncoder() + .encode("123456"); + return new InMemoryUserDetailsManager(User.withUsername("user") + .password(generatedPassword).roles("USER").build()); + } + + @Bean + @ConditionalOnMissingBean(AuthenticationEventPublisher.class) + DefaultAuthenticationEventPublisher defaultAuthenticationEventPublisher(ApplicationEventPublisher delegate) { + return new DefaultAuthenticationEventPublisher(delegate); + } + +} \ No newline at end of file diff --git a/spring-security/src/main/java/com/spring/config/Knife4jConfig.java b/spring-security/src/main/java/com/spring/config/Knife4jConfig.java new file mode 100644 index 0000000..24e8e45 --- /dev/null +++ b/spring-security/src/main/java/com/spring/config/Knife4jConfig.java @@ -0,0 +1,44 @@ +package com.spring.config; + +import io.swagger.v3.oas.models.ExternalDocumentation; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Contact; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.info.License; +import lombok.extern.slf4j.Slf4j; +import org.springdoc.core.models.GroupedOpenApi; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@Slf4j +public class Knife4jConfig { + + @Value("${server.port}") + private String port; + + @Bean + public OpenAPI openAPI() { + String url = "http://localhost:" + port; + + // 作者等信息 + Contact contact = new Contact().name("Bunny").email("1319900154@qq.com").url(url); + // 使用协议 + License license = new License().name("MIT").url("https://mit-license.org"); + // 相关信息 + Info info = new Info().title("Bunny-Admin") + .contact(contact).license(license) + .description("Bunny代码生成器") + .summary("Bunny的代码生成器") + .termsOfService(url) + .version("v1.0.0"); + + return new OpenAPI().info(info).externalDocs(new ExternalDocumentation()); + } + + @Bean + public GroupedOpenApi all() { + return GroupedOpenApi.builder().group("全部请求接口").pathsToMatch("/api/**").build(); + } +} diff --git a/spring-security/src/main/resources/application.yml b/spring-security/src/main/resources/application.yml new file mode 100644 index 0000000..8420b83 --- /dev/null +++ b/spring-security/src/main/resources/application.yml @@ -0,0 +1,12 @@ +server: + port: 8800 + +spring: + application: + name: spring-security + + +logging: + level: + com.spring: debug + root: info diff --git a/spring-security/src/main/resources/banner.txt b/spring-security/src/main/resources/banner.txt new file mode 100644 index 0000000..4509c36 --- /dev/null +++ b/spring-security/src/main/resources/banner.txt @@ -0,0 +1,11 @@ + + ____ ____ _ _ + | __ ) _ _ _ __ _ __ _ _ / ___|| |_ _ _ __| |_ _ + | _ \| | | | '_ \| '_ \| | | | \___ \| __| | | |/ _` | | | | + | |_) | |_| | | | | | | | |_| | ___) | |_| |_| | (_| | |_| | + |____/ \__,_|_| |_|_| |_|\__, | |____/ \__|\__,_|\__,_|\__, | + |___/ |___/ + +Service Name${spring.application.name} +SpringBoot Version: ${spring-boot.version}${spring-boot.formatted-version} +Spring Active:${spring.profiles.active} diff --git a/spring-security/src/main/resources/static/css/css2.css b/spring-security/src/main/resources/static/css/css2.css new file mode 100644 index 0000000..1659869 --- /dev/null +++ b/spring-security/src/main/resources/static/css/css2.css @@ -0,0 +1,108 @@ +/* devanagari */ +@font-face { + font-family: 'Poppins'; + font-style: normal; + font-weight: 300; + font-display: swap; + src: url(https://fonts.gstatic.com/s/poppins/v23/pxiByp8kv8JHgFVrLDz8Z11lFc-K.woff2) format('woff2'); + unicode-range: U+0900-097F, U+1CD0-1CF9, U+200C-200D, U+20A8, U+20B9, U+20F0, U+25CC, U+A830-A839, U+A8E0-A8FF, U+11B00-11B09; +} +/* latin-ext */ +@font-face { + font-family: 'Poppins'; + font-style: normal; + font-weight: 300; + font-display: swap; + src: url(https://fonts.gstatic.com/s/poppins/v23/pxiByp8kv8JHgFVrLDz8Z1JlFc-K.woff2) format('woff2'); + unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF; +} +/* latin */ +@font-face { + font-family: 'Poppins'; + font-style: normal; + font-weight: 300; + font-display: swap; + src: url(https://fonts.gstatic.com/s/poppins/v23/pxiByp8kv8JHgFVrLDz8Z1xlFQ.woff2) format('woff2'); + unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; +} +/* devanagari */ +@font-face { + font-family: 'Poppins'; + font-style: normal; + font-weight: 400; + font-display: swap; + src: url(https://fonts.gstatic.com/s/poppins/v23/pxiEyp8kv8JHgFVrJJbecmNE.woff2) format('woff2'); + unicode-range: U+0900-097F, U+1CD0-1CF9, U+200C-200D, U+20A8, U+20B9, U+20F0, U+25CC, U+A830-A839, U+A8E0-A8FF, U+11B00-11B09; +} +/* latin-ext */ +@font-face { + font-family: 'Poppins'; + font-style: normal; + font-weight: 400; + font-display: swap; + src: url(https://fonts.gstatic.com/s/poppins/v23/pxiEyp8kv8JHgFVrJJnecmNE.woff2) format('woff2'); + unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF; +} +/* latin */ +@font-face { + font-family: 'Poppins'; + font-style: normal; + font-weight: 400; + font-display: swap; + src: url(https://fonts.gstatic.com/s/poppins/v23/pxiEyp8kv8JHgFVrJJfecg.woff2) format('woff2'); + unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; +} +/* devanagari */ +@font-face { + font-family: 'Poppins'; + font-style: normal; + font-weight: 600; + font-display: swap; + src: url(https://fonts.gstatic.com/s/poppins/v23/pxiByp8kv8JHgFVrLEj6Z11lFc-K.woff2) format('woff2'); + unicode-range: U+0900-097F, U+1CD0-1CF9, U+200C-200D, U+20A8, U+20B9, U+20F0, U+25CC, U+A830-A839, U+A8E0-A8FF, U+11B00-11B09; +} +/* latin-ext */ +@font-face { + font-family: 'Poppins'; + font-style: normal; + font-weight: 600; + font-display: swap; + src: url(https://fonts.gstatic.com/s/poppins/v23/pxiByp8kv8JHgFVrLEj6Z1JlFc-K.woff2) format('woff2'); + unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF; +} +/* latin */ +@font-face { + font-family: 'Poppins'; + font-style: normal; + font-weight: 600; + font-display: swap; + src: url(https://fonts.gstatic.com/s/poppins/v23/pxiByp8kv8JHgFVrLEj6Z1xlFQ.woff2) format('woff2'); + unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; +} +/* devanagari */ +@font-face { + font-family: 'Poppins'; + font-style: normal; + font-weight: 700; + font-display: swap; + src: url(https://fonts.gstatic.com/s/poppins/v23/pxiByp8kv8JHgFVrLCz7Z11lFc-K.woff2) format('woff2'); + unicode-range: U+0900-097F, U+1CD0-1CF9, U+200C-200D, U+20A8, U+20B9, U+20F0, U+25CC, U+A830-A839, U+A8E0-A8FF, U+11B00-11B09; +} +/* latin-ext */ +@font-face { + font-family: 'Poppins'; + font-style: normal; + font-weight: 700; + font-display: swap; + src: url(https://fonts.gstatic.com/s/poppins/v23/pxiByp8kv8JHgFVrLCz7Z1JlFc-K.woff2) format('woff2'); + unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF; +} +/* latin */ +@font-face { + font-family: 'Poppins'; + font-style: normal; + font-weight: 700; + font-display: swap; + src: url(https://fonts.gstatic.com/s/poppins/v23/pxiByp8kv8JHgFVrLCz7Z1xlFQ.woff2) format('woff2'); + unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; +} diff --git a/spring-security/src/main/resources/static/error/400.html b/spring-security/src/main/resources/static/error/400.html new file mode 100644 index 0000000..fc1e212 --- /dev/null +++ b/spring-security/src/main/resources/static/error/400.html @@ -0,0 +1,69 @@ + + + + + 400 错误 - phpstudy + + + + + + + + + + + + + + + +
+
+

HTTP 400 - Bad Request

+
+
错误说明:因为错误的语法导致服务器无法理解请求信息。
+
原因1:客户端发起的请求不符合服务器对请求的某些限制,或者请求本身存在一定的错误。
+
解决办法:
+
链接中有特殊字符或者链接长度过长导致,请对应修改.
+
原因2:request header 或者 cookie 过大所引起
+
解决办法:
+
crtl+shift+delete 快捷键清除cookie.
+
+
+
+ + \ No newline at end of file diff --git a/spring-security/src/main/resources/static/error/403.html b/spring-security/src/main/resources/static/error/403.html new file mode 100644 index 0000000..6c281b0 --- /dev/null +++ b/spring-security/src/main/resources/static/error/403.html @@ -0,0 +1,69 @@ + + + + + 403 错误 - phpstudy + + + + + + + + + + + + + + + +
+
+

403 - Forbidden 禁止访问: 访问被拒绝

+
+
错误说明:禁止访问,服务器拒绝访问
+
原因1:未找到默认的索引文件
+
解决办法:
+
IIS中【启用默认内容文档】选项中将默认打开文档修改为程序首页文件格式,如:index.html或者index.php
+
原因2:文件夹安全权限导致
+
解决办法:
+
程序文件-右击-属性-安全-Users-修改为读取和执行权限
+
+
+
+ + \ No newline at end of file diff --git a/spring-security/src/main/resources/static/error/404.html b/spring-security/src/main/resources/static/error/404.html new file mode 100644 index 0000000..6cc5d1e --- /dev/null +++ b/spring-security/src/main/resources/static/error/404.html @@ -0,0 +1,78 @@ + + + + + 404 错误 - phpstudy + + + + + + + + + + + + + + + +
+
+

404 - Page Not Found 未找到

+
+
错误说明:请求的页面不存在
+
原因1:访问的文档权限不够
+
解决办法:
+
修改文件权限为755,windos系统修改目录权限为可写可读。
+
原因2:防火墙的原因
+
解决办法:
+
先关闭让防火墙通过WWW服务。
+
原因3:站点根目录无默认访问文件
+
解决办法:
+
在根目录中创建index.html或者创建index.php。
+
原因4:站点配置目录不正确
+
解决办法:
+
将网站应用程序复制到站点目录中,或者修改站点配置目录指定到应用程序目录中。
+
原因5:站点使用了伪静态
+
解决办法:
+
将伪静态规则删除,或者重新编写正确的伪静态规则,或关闭伪静态配置。
+
+
+
+ + \ No newline at end of file diff --git a/spring-security/src/main/resources/static/error/500.html b/spring-security/src/main/resources/static/error/500.html new file mode 100644 index 0000000..6071bf4 --- /dev/null +++ b/spring-security/src/main/resources/static/error/500.html @@ -0,0 +1,64 @@ + + + + + 500 - 服务器错误 + + + + +
+

:'(

+

服务器开小差啦!管理员正在修理中...

+

还请阁下静候站点恢复~

+
+ + \ No newline at end of file diff --git a/spring-security/src/main/resources/static/error/501.html b/spring-security/src/main/resources/static/error/501.html new file mode 100644 index 0000000..48ac544 --- /dev/null +++ b/spring-security/src/main/resources/static/error/501.html @@ -0,0 +1,66 @@ + + + + + 501 错误 - phpstudy + + + + + + + + + + + + + + + +
+
+

HTTP 501 - Not Implemented

+
+
错误说明:服务器没有相应的执行动作来完成当前请求。
+
原因1:Web 服务器不支持实现此请求所需的功能
+
解决办法:
+
可以用来HttpWebRequest指定一个UserAgent来试试的,有时候你可以换电脑来测试一下的。
+
+
+
+ + \ No newline at end of file diff --git a/spring-security/src/main/resources/static/error/502.html b/spring-security/src/main/resources/static/error/502.html new file mode 100644 index 0000000..ea44be2 --- /dev/null +++ b/spring-security/src/main/resources/static/error/502.html @@ -0,0 +1,80 @@ + + + + + 502 错误 - phpstudy + + + + + + + + + + + + + + + +
+
+

HTTP 502 - Bad Gateway 没有响应

+
+
错误说明:坏的网关,http向后端节点请求,没有响应
+
原因1:DNS 缓冲
+
解决办法:
+
在dos窗口运行 ipconfig /flushdns,该命令会刷新DNS缓冲。
+
原因2:浏览器代理
+
解决办法:
+
关掉代理。
+
原因3:dns 被劫持了,即使使用国外的dns,也会被劫持
+
解决办法:
+
+ 去掉VPN服务器的DNS。切换另外的dns。在windows系统中,可以在本地网络连接的属性中,去掉默认的dns,选用国外的dns,比如google的或opendns。 +
+
原因4:php执行超时
+
解决办法:
+
修改/usr/local/php/etc/php.ini 将max_execution_time 改为300。
+
原因5:nginx等待时间超时
+
解决办法:
+
适当增加nginx.conf配置文件中FastCGI的timeout时间。
+
+
+
+ + \ No newline at end of file diff --git a/spring-security/src/main/resources/static/error/503.html b/spring-security/src/main/resources/static/error/503.html new file mode 100644 index 0000000..c6fec86 --- /dev/null +++ b/spring-security/src/main/resources/static/error/503.html @@ -0,0 +1,69 @@ + + + + + 503 错误 - phpstudy + + + + + + + + + + + + + + + +
+
+

HTTP 503 - Service Unavailable 服务不可用

+
+
错误说明:服务当前不可用
+
原因1:服务不可用状态
+
解决办法:
+
服务器或许就是正在维护或者暂停了,你可以联系一下服务器空间商。
+
原因2:程序占用资源太多
+
解决办法:
+
通过设置应用程序池把账户改为NetworkService即可解决。
+
+
+
+ + \ No newline at end of file diff --git a/spring-security/src/main/resources/static/error/504.html b/spring-security/src/main/resources/static/error/504.html new file mode 100644 index 0000000..2980dd0 --- /dev/null +++ b/spring-security/src/main/resources/static/error/504.html @@ -0,0 +1,81 @@ + + + + + 504 错误 - phpstudy + + + + + + + + + + + + + + + +
+
+

HTTP 504 - Gateway Timeout 网关超时

+
+
错误说明:网关超时,服务器响应时间,达到超出设定的范围
+
原因1:后端电脑之间 IP 通讯缓慢而产生
+
解决办法:
+
如果您的 Web 服务器由某一网站托管, 只有负责那个网站设置的人员才能解决这个问题。
+
原因2:由于nginx默认的fastcgi进程响应的缓冲区太小造成的错误
+
解决办法:
+
一般默认的fastcgi进程响应的缓冲区是8K,这时可以设置大一点,在nginx.conf里,加入:fastcgi_buffers 8 + 128k这表示设置fastcgi缓冲区为8块128k大小的空间。当然如果在进行某一项即时的操作, 可能需要nginx的超时参数调大点, + 例如设置成60秒:send_timeout 60;经过这两个参数的调整,一般不会再提示“504 Gateway Time-out”错误,问题基本解决。 +
+
原因3:PHP环境的配置问题
+
解决办法:
+
更改php-fpm的几处配置: 把max_children由之前的10改为现在的30,这样就可以保证有充足的php-cgi进程可以被使用; + 把request_terminate_timeout由之前的0s改为60s,这样php-cgi进程 处理脚本的超时时间就是60秒,可以防止进程都被挂起,提高利用效率。 + 接着再更改nginx的几个配置项,减少FastCGI的请求次数,尽量维持buffers不变: fastcgi_buffers由 4 64k 改为 2 + 256k; fastcgi_buffer_size 由 64k 改为 128K; fastcgi_busy_buffers_size 由 128K 改为 256K; + fastcgi_temp_file_write_size 由 128K 改为 256K。 重新加载php-fpm和nginx的配置,再次测试,如果没有出现“504 + Gateway Time-out”错误,问题解决。 +
+
+
+
+ + \ No newline at end of file diff --git a/spring-security/src/main/resources/static/error/505.html b/spring-security/src/main/resources/static/error/505.html new file mode 100644 index 0000000..1cd5dd5 --- /dev/null +++ b/spring-security/src/main/resources/static/error/505.html @@ -0,0 +1,72 @@ + + + + + 505 错误 - phpstudy + + + + + + + + + + + + + + + +
+
+

HTTP 505 - HTTP Version Not Supported

+
+
错误说明:HTTP 版本不受支持。
+
原因1:您的 Web 服务器不支持,或拒绝支持客户端(如您的浏览器)在发送给服务器的 HTTP 请求数据流中指定的 HTTP + 协议版本 +
+
解决办法:
+
升级您的 Web 服务器软件。
+
原因2:http请求格式的错误
+
解决办法:
+
对照一下自己的代码,从打印的信息中终于找到问题所在。可能在请求后面多加了一个空格。http协议真是很严格了。 +
+
+
+
+ + \ No newline at end of file diff --git a/spring-security/src/main/resources/static/error/506.html b/spring-security/src/main/resources/static/error/506.html new file mode 100644 index 0000000..8160abd --- /dev/null +++ b/spring-security/src/main/resources/static/error/506.html @@ -0,0 +1,66 @@ + + + + + 506 错误 - phpstudy + + + + + + + + + + + + + + + +
+
+

HTTP 506 - Variant Also Negotiates

+
+
错误说明:
+
原因1:服务器存在内部配置错误
+
解决办法:
+
被请求的协商变元资源被配置为在透明内容协商中使用自己,因此在一个协商处理中不是一个合适的重点。
+
+
+
+ + \ No newline at end of file diff --git a/spring-security/src/main/resources/static/error/507.html b/spring-security/src/main/resources/static/error/507.html new file mode 100644 index 0000000..fb6c7e7 --- /dev/null +++ b/spring-security/src/main/resources/static/error/507.html @@ -0,0 +1,66 @@ + + + + + 507 错误 - phpstudy + + + + + + + + + + + + + + + +
+
+

HTTP 507 - Insufficient Storage

+
+
错误说明:
+
原因1:服务器无法存储完成请求所必须的内容
+
解决办法:
+
这个状况被认为是临时的。WebDAV (RFC 4918)。
+
+
+
+ + \ No newline at end of file diff --git a/spring-security/src/main/resources/static/error/509.html b/spring-security/src/main/resources/static/error/509.html new file mode 100644 index 0000000..520abbd --- /dev/null +++ b/spring-security/src/main/resources/static/error/509.html @@ -0,0 +1,66 @@ + + + + + 509 错误 - phpstudy + + + + + + + + + + + + + + + +
+
+

HTTP 509 - Bandwidth Limit Exceeded

+
+
错误说明:
+
原因1:网站流量已经超出您所购买的方案限制即服务器达到带宽限制
+
解决办法:
+
1.升级方案 2.等到下个月后流量重新计算,网站即可正常浏览。
+
+
+
+ + \ No newline at end of file diff --git a/spring-security/src/main/resources/static/error/510.html b/spring-security/src/main/resources/static/error/510.html new file mode 100644 index 0000000..95b2f9f --- /dev/null +++ b/spring-security/src/main/resources/static/error/510.html @@ -0,0 +1,66 @@ + + + + + 510 错误 - phpstudy + + + + + + + + + + + + + + + +
+
+

HTTP 510 - Not Extended

+
+
错误说明:
+
原因1:获取资源所需要的策略并没有被满足
+
解决办法:
+
需要请求有额外的扩展内容,服务器才能处理请求。
+
+
+
+ + \ No newline at end of file diff --git a/spring-security/src/main/resources/static/favicon.ico b/spring-security/src/main/resources/static/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..17f9dd11abe93a4170a7879244d0fea91142ca9a GIT binary patch literal 67758 zcmeHQ2~<>9n#QOskxfP8lEoGZ6WbVd+`%2xxF8CmAXGsWs|&fP`E-hco5-T(g2%c~Fkd*>Z5_ex~9vFa#I^3;~7!Lx3T`5MT%}1Q-Gg0fqoWfFZyTUvFa#I^9z~$R~29F#ZHGEw75-^y%mI!>a8U+Tgl&mIJ5><-f8@-UFJ?ng9rFnGhdO z(myrY-?1OiLA-EhP2rUxzWxlq9)Q45pFrqM3RV7i% z=0^2^=CdXM0)-KodDI7>{hi=I8;k|7u6Up6K|O>$V}qU@{2=9Uix%uk?eB;KFfJg< zjOEOP z=|MX#w4o+U?atd@>tNR*&B=$alZ?-aI;~2yRg%`UNn!LRUQAdRk(EbGJ^=lI*cVvpL^ywNb;UNx^dSF!zFt4a7S!pI50J;; z@2ztH`M~)B0-vi}^k&V#{@o^Qafwy~`ucd)#S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%y4^c$rY^W>dN0RzhK{@8CiKZR~KDwN-VwC zoLJiOQ$k_g)(P?1AwB`_DTd(6iFNUJFunsW1j7dRf9P_vn_ByfZ?Jbm^5Gi>tXXE0 z1D0b0j05Dd&ead%?*9+~_kRcowpZ)ebfWBm#}Fu*wIQ#)4-oqSGY4=i_#bf}yQ~L! ztRd{cE+H^B`1!#Pl~pY|u&Y{ynh@Ha-@xvsaRJ8x4U7*+)@hgZAeT;(eaWK;R4>Xe zz?wvy{T*`vW5zR!-*!n4@@PZY!CgWiYUBhN)UAD_hiNBV(u1s?0DDnV1n7FPHG3~mO_=Wotoi}u0mlKXSHE@AT9@=7NgK<4 z+$sWFCMCv`^x{fVMxs{Qc7YPxY1fY}wCy0s+;F zi{=0ans7YO25~;tGR6&Ke!Txd{fZBdLXK;fA+uF zHH|zJZXl6d35;x1T!?&5p$oUvFa#I^3;~7!Lx3T`5MT%}1Q-Gg0fqoW qfFZyTUd_qf*=C` literal 0 HcmV?d00001 diff --git a/spring-security/src/main/resources/templates/index.html b/spring-security/src/main/resources/templates/index.html new file mode 100644 index 0000000..0919378 --- /dev/null +++ b/spring-security/src/main/resources/templates/index.html @@ -0,0 +1,502 @@ + + + + + + Spring Security 6 学习中心 + + + + + + + + + +
+
+ +
+
+ + +
+
+
+

掌握 Spring Security 6

+

学习最强大的Java安全框架,保护您的应用程序免受现代安全威胁。Spring Security + 6提供了全面的身份验证和授权功能,让您的应用安全无忧。

+ +
+
+
+ + +
+
+
+

Spring Security 6 核心特性

+

Spring Security 6引入了许多强大的新功能,使应用程序安全比以往任何时候都更简单、更强大。

+
+
+
+
+ +
+

现代化的认证

+

支持OAuth 2.0、OpenID Connect、SAML 2.0等多种认证协议,满足现代应用的安全需求。

+
+
+
+ +
+

强大的授权

+

细粒度的权限控制,支持方法级安全、领域对象安全等多种授权模式。

+
+
+
+ +
+

防护机制

+

内置CSRF防护、点击劫持防护、内容安全策略等安全机制,保护应用免受常见攻击。

+
+
+
+
+ + +
+
+
+

学习资源与文档

+

探索我们的文档和工具,快速掌握Spring Security 6的强大功能。

+
+
+
+
+
+

API 文档

+

详细的API参考文档,包含所有类、方法和配置选项的详细说明,帮助您充分利用Spring Security + 6的所有功能。

+ 查看API文档 +
+
+
+
+
+

Swagger UI

+

交互式API文档,可以直接在浏览器中测试API端点,查看请求和响应示例,加快开发流程。

+ 访问Swagger UI +
+
+
+
+
+ + + + + \ No newline at end of file diff --git a/spring-security/src/test/java/com/spring/SpringSecurityApplicationTests.java b/spring-security/src/test/java/com/spring/SpringSecurityApplicationTests.java new file mode 100644 index 0000000..1463f61 --- /dev/null +++ b/spring-security/src/test/java/com/spring/SpringSecurityApplicationTests.java @@ -0,0 +1,13 @@ +package com.spring; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SpringSecurityApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/spring-security/src/test/java/com/spring/context/SecurityContextHolderTest.java b/spring-security/src/test/java/com/spring/context/SecurityContextHolderTest.java new file mode 100644 index 0000000..90b7fd3 --- /dev/null +++ b/spring-security/src/test/java/com/spring/context/SecurityContextHolderTest.java @@ -0,0 +1,24 @@ +package com.spring.context; + +import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Test; +import org.springframework.security.authentication.TestingAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; + +public class SecurityContextHolderTest { + + @Test + void testSecurityContextHolder() { + SecurityContext context = SecurityContextHolder.createEmptyContext(); + Authentication authentication = new TestingAuthenticationToken("username", "password", "ROLE_USER"); + context.setAuthentication(authentication); + + SecurityContextHolder.setContext(context); + + SecurityContext securityContext = SecurityContextHolder.getContext(); + Authentication contextAuthentication = securityContext.getAuthentication(); + System.out.println(JSON.toJSON(contextAuthentication)); + } +} diff --git a/spring-security/src/test/java/com/spring/password/PasswordTest.java b/spring-security/src/test/java/com/spring/password/PasswordTest.java new file mode 100644 index 0000000..cb6c0e0 --- /dev/null +++ b/spring-security/src/test/java/com/spring/password/PasswordTest.java @@ -0,0 +1,54 @@ +package com.spring.password; + +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.crypto.encrypt.BytesEncryptor; +import org.springframework.security.crypto.encrypt.Encryptors; +import org.springframework.security.crypto.encrypt.TextEncryptor; +import org.springframework.security.crypto.factory.PasswordEncoderFactories; +import org.springframework.security.crypto.keygen.KeyGenerators; +import org.springframework.security.crypto.password.PasswordEncoder; + +@Slf4j +public class PasswordTest { + + @Test + void passwordEncoderTest() { + PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder(); + + String encode = passwordEncoder.encode("123456"); + log.info("PasswordEncoder 加密密码:{}", encode); + + boolean matches = passwordEncoder.matches("123456", encode); + log.info("PasswordEncoder 是否匹配:{}", matches); + } + + @Test + void UserDetailTest() { + UserDetails user = User.builder() + .username("user") + .password("123456") + .roles("user") + .build(); + UserDetails password = User.withUserDetails(user).password("123456").build(); + System.out.println(password.getPassword());// 123456 + } + + @Test + void EncryptorsTest() { + String salt = KeyGenerators.string().generateKey(); // generates a random 8-byte salt that is then hex-encoded + String string = "password"; + BytesEncryptor bytesEncryptor = Encryptors.stronger(string, salt); + byte[] encrypted = bytesEncryptor.encrypt(string.getBytes()); + byte[] decrypt = bytesEncryptor.decrypt(string.getBytes()); + + TextEncryptor password = Encryptors.text(string, salt); + String encrypt = password.encrypt(string); + String decrypted = password.decrypt(encrypt); + System.out.println(encrypt); + System.out.println(decrypted); + } + +}