Springboot2.3集成Spring security框架的案例

  介绍

这篇文章主要介绍Springboot2.3集成Spring security框架的案例,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

& lt;及# 63;xml version=?.0”;编码=癠TF-8", # 63;比;   http://maven.apache.org/POM/4.0.0" & lt;项目xmlns=?xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance"   ,xsi: schemaLocation=癶ttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"的在;   & lt; modelVersion> 4.0.0   & lt; parent>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-parent   & lt; version> 2.3.0.RELEASE   & lt; relativePath/比;& lt; !——从库中查找父——比;   & lt;/parent>   & lt; groupId> com.jack   & lt; artifactId> demo   & lt; version> 0.0.1-SNAPSHOT   & lt; packaging> war   & lt; name> demo   春天Security< & lt; description>演示项目;/description>      & lt; properties>   & lt; java.version> 1.8 & lt;/java.version>   & lt;/properties>      & lt; dependencies>   & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-security   & lt;/dependency>   & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-web   & lt;/dependency>      & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-tomcat   & lt; scope> provided   & lt;/dependency>   & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-test   & lt; scope> test   & lt; exclusions>   & lt; exclusion>   & lt; groupId> org.junit.vintage   & lt; artifactId> junit-vintage-engine   & lt;/exclusion>   & lt;/exclusions>   & lt;/dependency>   & lt; dependency>   & lt; groupId> org.springframework.security   & lt; artifactId> spring-security-test   & lt; scope> test   & lt;/dependency>   & lt;/dependencies>      & lt; build>   & lt; plugins>   & lt; plugin>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-maven-plugin   & lt;/plugin>   & lt;/plugins>   & lt;/build>      & lt;/project>

//手动定义用户认证和//关联用户服务认证二者取一

这里测试用的是手动定义用户认证! ! !

包com.jack.demo;
  
  进口org.springframework.beans.factory.annotation.Autowired;
  进口org.springframework.context.annotation.Configuration;
  进口org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  进口org.springframework.security.config.annotation.web.builders.HttpSecurity;
  进口org.springframework.security.config.annotation.web.builders.WebSecurity;
  进口org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  进口org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  进口org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;/* *
  * @program:演示
  * @description:安全配置
  * @author: Jack.Fang
  * @date: 2020-06-01 1541
  * */@ configuration
  @EnableWebSecurity
  公开课SpringSecurityConfig延伸WebSecurityConfigurerAdapter {
  
  @ autowired
  私人MyUserService MyUserService;
  
  @Override
  保护无效配置(AuthenticationManagerBuilder auth)抛出异常{//手动定义用户认证
  auth.inMemoryAuthentication ()。passwordEncoder(新BCryptPasswordEncoder ()) .withUser (“admin")。密码(新BCryptPasswordEncoder () .encode (“123456“)) .roles (“ADMIN");
  auth.inMemoryAuthentication ()。passwordEncoder(新BCryptPasswordEncoder ()) .withUser (“jack")。密码(新BCryptPasswordEncoder () .encode (“fang")) .roles (“USER");//关联用户服务认证//auth.userDetailsService (myUserService)。passwordEncoder(新MyPasswordEncoder ());//默认jdbc认证//auth.jdbcAuthentication () .usersByUsernameQuery .authoritiesByUsernameQuery (“;”) (“;”)。passwordEncoder(新MyPasswordEncoder ());
  }
  
  @Override
  保护无效配置(HttpSecurity http){抛出异常
  http.authorizeRequests ()
  .antMatchers (“/? .permitAll ()
  .authenticated .anyRequest () ()
  ,()
  .permitAll .logout () ()
  ,()
  .formLogin ();
  .disable http.csrf () ();
  }
  
  @Override
  公共空间配置(WebSecurity web)抛出异常{
  web.ignoring () .antMatchers (“/js/* *”,“/css/* *”、“/图像/* *“);
  }
  }

Springboot2.3集成Spring security框架的案例