如何在springboot中集成springsecurity?
如何在spring boot中集成spring security?针对这个问题,今天小编总结这篇有关spring security集成的文章,希望能帮助更多想解决这个问题的朋友找到更加简单易行的办法。
创新互联建站专注于赤坎网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供赤坎营销型网站建设,赤坎网站制作、赤坎网页设计、赤坎网站官网定制、成都小程序开发服务,打造赤坎网络公司原创品牌,更为您提供赤坎网站排名全网营销落地服务。
1.1 创建mavean工程springboot_security
Pom.xml
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.6.RELEASEversion>
parent>
<properties>
<project.build.sourceEncoding>UTF‐8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
dependency>
<dependency>
<groupId>org.apache.tomcat.embedgroupId>
<artifactId>tomcat-embed-jasperartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.0version>
dependency>
dependencies>
<build>
<finalName>security-springbootfinalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
<plugin>
<artifactId>maven-resources-pluginartifactId>
<configuration>
<encoding>utf-8encoding>
<useDefaultDelimiters>trueuseDefaultDelimiters>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
<filtering>truefiltering>
<includes>
<include>**/*include>
includes>
resource>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
resource>
resources>
configuration>
plugin>
plugins>
pluginManagement>
build>
1.2 在resources下添加springboot配置文件,application.properties
server.port=8080
1.3 创建springboot启动类
@SpringBootApplication
public classsecurityApplication {
public static voidmain(String[] args){
SpringApplication.run(securityApplication.class,args);
}
}
1.4 创建springmvc配置类:
@Configuration
public classWebConfigimplementsWebMvcConfigurer {
@Override
public voidaddViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("redirect:/login");
}
}
1.5 在application.properties中配置视图解析器的属性:
spring.mvc.view.prefix=/WEB‐INF/views/
spring.mvc.view.suffix=.jsp
1.6 创建spring security的配置文件
@Configuration
public classWebSecurityConfig extendsWebSecurityConfigurerAdapter {
//配置用户信息服务
@Bean
publicUserDetailsService userDetailsService() {
InMemoryUserDetailsManager manager =newInMemoryUserDetailsManager();
manager.createUser(User.withUsername("zhangsan").password("123").authorities("p1").build());
manager.createUser(User.withUsername("lisi").password("456").authorities("p2").build());
returnmanager;
}
@Bean
publicPasswordEncoder passwordEncoder() {
returnNoOpPasswordEncoder.getInstance();
}
//配置安全拦截机制
@Override
protected voidconfigure(HttpSecurity http)throwsException {
http.authorizeRequests()
.antMatchers("/r/r1").hasAuthority("p1")
.antMatchers("/r/r2").hasAuthority("p2")
.antMatchers("/r/**").authenticated()//所有/r/**的请求必须认证通过
.anyRequest().permitAll()//除了/r/**,其它的请求可以访问
.and()
.formLogin()//允许表单登录
.successForwardUrl("/login-success");//自定义登录成功的页面地址
}
}
1.7 创建控制器:LoginController
@RestController
public classLoginController {
@RequestMapping(value ="/login-success",produces = {"text/plain;charset=UTF-8"})
publicString loginSuccess(){
return"登录成功";
}
/**
*测试资源1
* @return
*/
@GetMapping(value ="/r/r1",produces = {"text/plain;charset=UTF-8"})
publicString r1(){
return"访问资源1";
}
/**
*测试资源2
* @return
*/
@GetMapping(value ="/r/r2",produces = {"text/plain;charset=UTF-8"})
publicString r2(){
return"访问资源2";
}
}
测试
看完上述内容,你们掌握在spring boot中集成spring security的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
新闻标题:如何在springboot中集成springsecurity?
当前地址:http://scyanting.com/article/ihejij.html