上一篇文章《Spring Security(一)–Architecture Overview》,我们先容了Spring Security的基本架构,这一节我们通过Spring官方给出的一个guides例子,来相识Spring Security是如何掩护我们的应用的,之后会对举办一个解读。
2 Spring Security Guides
2.1 引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
由于我们集成了springboot,所以不需要显示的引入Spring Security文档中描写core,config依赖,只需要引入spring-boot-starter-security即可。
2.2 建设一个不受安详限制的web应用
这是一个首页,不受安详限制
src/main/resources/templates/home.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example</title>
</head>
<body>
<p>Welcome!</p>
<p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p>
</body>
</html>
这个简朴的页面上包括了一个链接,跳转到”/hello”。对应如下的页面
src/main/resources/templates/hello.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<p>Hello world!</p>
</body>
</html>
接下来设置Spring MVC,使得我们可以或许会见到页面。
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("home");
registry.addViewController("/").setViewName("home");
registry.addViewController("/hello").setViewName("hello");
registry.addViewController("/login").setViewName("login");
}
}
2.3 设置Spring Security
一个典范的安详设置如下所示:
@Configuration
@EnableWebSecurity <1>
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { <1>
@Override
protected void configure(HttpSecurity http) throws Exception {
http <2>
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth <3>
.inMemoryAuthentication()
.withUser("admin").password("admin").roles("USER");
}
}
<1> @EnableWebSecurity注解使得SpringMVC集成了Spring Security的web安详支持。别的,WebSecurityConfig设置类同时集成了WebSecurityConfigurerAdapter,重写了个中的特定要领,用于自界说Spring Security设置。整个Spring Security的事情量,其实都是会合在该设置类,不只仅是这个guides,软件开发,实际项目中也是如此。
<2> configure(HttpSecurity)界说了哪些URL路径应该被拦截,如字面意思所描写:”/“, “/home”答允所有人会见,劳务派遣管理系统,”/login”作为登录进口,也被答允会见,而剩下的”/hello”则需要登岸后才可以会见。
<3> configureGlobal(AuthenticationManagerBuilder)在内存中设置一个用户,admin/admin别离是用户名和暗码,这个用户拥有USER脚色。
我们今朝还没有登录页面,下面建设登录页面:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example </title>
</head>
<body>
<div th:if="${param.error}">
Invalid username and password.
</div>
<div th:if="${param.logout}">
You have been logged out.
</div>
<form th:action="@{/login}" method="post">
<div><label> User Name : <input type="text" name="username"/> </label></div>
<div><label> Password: <input type="password" name="password"/> </label></div>
<div><input type="submit" value="Sign In"/></div>
</form>
</body>
</html>