- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>3.1.5version>
- <relativePath/>
- parent>
- <groupId>com.examplegroupId>
- <artifactId>demoartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>demoname>
- <description>Demo project for Spring Bootdescription>
- <properties>
- <java.version>21java.version>
- properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-securityartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>org.springframework.securitygroupId>
- <artifactId>spring-security-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <configuration>
- <image>
- <builder>paketobuildpacks/builder-jammy-base:latestbuilder>
- image>
- configuration>
- plugin>
- plugins>
- build>
-
- project>
- /**
- *
- */
- package com.example.demo;
-
- import java.io.IOException;
-
- import org.springframework.security.authentication.BadCredentialsException;
- import org.springframework.security.authentication.InternalAuthenticationServiceException;
- import org.springframework.security.authentication.ProviderManager;
- import org.springframework.security.core.Authentication;
- import org.springframework.security.core.AuthenticationException;
- import org.springframework.security.core.context.SecurityContextHolder;
- import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
- import org.springframework.security.web.util.matcher.RequestMatcher;
-
- import jakarta.servlet.FilterChain;
- import jakarta.servlet.ServletException;
- import jakarta.servlet.ServletRequest;
- import jakarta.servlet.ServletResponse;
- import jakarta.servlet.http.HttpServletRequest;
- import jakarta.servlet.http.HttpServletResponse;
-
- /**
- *
- */
- public class AuthenticationBuilderFilter extends AbstractAuthenticationProcessingFilter {
-
-
- protected AuthenticationBuilderFilter() {
- super(new RequestMatcher() {
- @Override
- public boolean matches(HttpServletRequest request) {
- return true;
- }
- });
- super.setAuthenticationManager(new ProviderManager(new WebAuthenticationProvider()));
- }
-
- @Override
- public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
- throws AuthenticationException, IOException, ServletException {
- Authentication auth = new WebAuthentication(request);
- return getAuthenticationManager().authenticate(auth);
- }
-
- @Override
- public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
- throws IOException, ServletException {
- HttpServletRequest request = (HttpServletRequest) req;
- HttpServletResponse response = (HttpServletResponse) res;
- if (!requiresAuthentication(request, response)) {
- chain.doFilter(request, response);
- return;
- }
- try {
- Authentication authenticationResult = attemptAuthentication(request, response);
- if (authenticationResult == null) {
- throw new BadCredentialsException("没有身份信息");
- }
- SecurityContextHolder.getContext().setAuthentication(authenticationResult);
- chain.doFilter(request, response);
- }
- catch (InternalAuthenticationServiceException failed) {
- this.logger.error("An internal error occurred while trying to authenticate the user.", failed);
- unsuccessfulAuthentication(request, response, failed);
- }
- catch (AuthenticationException ex) {
- // Authentication failed
- unsuccessfulAuthentication(request, response, ex);
- }
- }
-
-
- }
这里有几个地方需要注意(敲黑板啦~~)
- /**
- *
- */
- package com.example.demo;
-
- import java.util.Collection;
-
- import org.springframework.security.core.Authentication;
- import org.springframework.security.core.GrantedAuthority;
-
- import jakarta.servlet.http.HttpServletRequest;
-
- /**
- *
- */
- public class WebAuthentication implements Authentication{
-
- public WebAuthentication() {
-
- }
-
- public WebAuthentication(HttpServletRequest request) {
-
- }
-
- /**
- *
- */
- private static final long serialVersionUID = -1705541938861263059L;
-
- @Override
- public String getName() {
- return null;
- }
-
- @Override
- public Collection extends GrantedAuthority> getAuthorities() {
- return null;
- }
-
- @Override
- public Object getCredentials() {
- return null;
- }
-
- @Override
- public Object getDetails() {
- return null;
- }
-
- @Override
- public Object getPrincipal() {
- return null;
- }
-
- @Override
- public boolean isAuthenticated() {
- return false;
- }
-
- @Override
- public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
-
- }
- }
- /**
- *
- */
- package com.example.demo;
-
- import org.springframework.security.authentication.AuthenticationProvider;
- import org.springframework.security.core.Authentication;
- import org.springframework.security.core.AuthenticationException;
-
- /**
- *
- */
- public class WebAuthenticationProvider implements AuthenticationProvider {
-
- @Override
- public Authentication authenticate(Authentication authentication) throws AuthenticationException {
- authentication.setAuthenticated(true);
- return authentication;
- }
-
- @Override
- public boolean supports(Class> authentication) {
- return authentication.equals(WebAuthentication.class);
- }
-
- }
- /**
- *
- */
- package com.example.demo;
-
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.security.config.annotation.web.builders.HttpSecurity;
- import org.springframework.security.web.SecurityFilterChain;
- import org.springframework.security.web.csrf.CsrfFilter;
-
- /**
- *
- */
- @Configuration
- public class SecurityConfig {
-
- @Bean
- SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
- http
- .csrf(csrf ->csrf.disable())
- .addFilterAfter(new AuthenticationBuilderFilter(), CsrfFilter.class)
- ;
- return http.build();
- }
- }