企业分布式微服务云SpringCloud SpringBoot mybatis (六)Spring Boot中使用Spring Security进行安全控制

准备工做html

首先,构建一个简单的Web工程,以用于后续添加安全控制,也能够用以前Chapter3-1-2作为基础工程。若对如何使用Spring Boot构建Web应用,能够先阅读《Spring Boot开发Web应用》一文。web

Web层实现请求映射spring

@Controller public class HelloController {安全

@RequestMapping("/")
public String index() {
    return "index";
}

@RequestMapping("/hello")
public String hello() {
    return "hello";
}
复制代码

} /:映射到index.html /hello:映射到hello.html 实现映射的页面app

src/main/resources/templates/index.htmlide

Spring Security入门

欢迎使用Spring Security!

点击 这里 打个招呼吧spring-boot

src/main/resources/templates/hello.htmlweb安全

Hello World!

Hello world!

能够看到在index.html中提供到/hello的连接,显然在这里没有任何安全控制,因此点击连接后就能够直接跳转到hello.html页面。

整合Spring Securitypost

在这一节,咱们将对/hello页面进行权限控制,必须是受权用户才能访问。当没有权限的用户访问后,跳转到登陆页面。ui

添加依赖

在pom.xml中添加以下配置,引入对Spring Security的依赖。

... org.springframework.boot spring-boot-starter-security ... Spring Security配置

建立Spring Security的配置类WebSecurityConfig,具体以下:

@Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/", "/home").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
        .logout()
            .permitAll();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .inMemoryAuthentication()
            .withUser("user").password("password").roles("USER");
}
复制代码

}

经过@EnableWebSecurity注解开启Spring Security的功能 继承WebSecurityConfigurerAdapter,并重写它的方法来设置一些web安全的细节 configure(HttpSecurity http)方法 经过authorizeRequests()定义哪些URL须要被保护、哪些不须要被保护。例如以上代码指定了/和/home不须要任何认证就能够访问,其余的路径都必须经过身份验证。 经过formLogin()定义当须要用户登陆时候,转到的登陆页面。 configureGlobal(AuthenticationManagerBuilder auth)方法,在内存中建立了一个用户,该用户的名称为user,密码为password,用户角色为USER。 新增登陆请求与页面

在完成了Spring Security配置以后,咱们还缺乏登陆的相关内容。

HelloController中新增/login请求映射至login.html

@Controller public class HelloController {

// 省略以前的内容...

@RequestMapping("/login")
public String login() {
    return "login";
}
复制代码

} 新增登陆页面:src/main/resources/templates/login.html

Spring Security Example
用户名或密码错
您已注销成功
能够看到,实现了一个简单的经过用户名和密码提交到/login的登陆方式。

根据配置,Spring Security提供了一个过滤器来拦截请求并验证用户身份。若是用户身份认证失败,页面就重定向到/login?error,而且页面中会展示相应的错误信息。若用户想要注销登陆,能够经过访问/login?logout请求,在完成注销以后,页面展示相应的成功消息。

到这里,咱们启用应用,并访问http://localhost:8080/,能够正常访问。可是访问http://localhost:8080/hello的时候被重定向到了http://localhost:8080/login页面,由于没有登陆,用户没有访问权限,经过输入用户名user和密码password进行登陆后,跳转到了Hello World页面,再也经过访问http://localhost:8080/login?logout,就能够完成注销操做。

为了让整个过程更完成,咱们能够修改hello.html,让它输出一些内容,并提供“注销”的连接。

Hello World!

Hello [[${#httpServletRequest.remoteUser}]]!

源码来源:http://minglisoft.cn/honghu/technology.html

本文经过一个最简单的示例完成了对Web应用的安全控制,Spring Security提供的功能还远不止于此,更多Spring Security的使用可参见Spring Security Reference。
相关文章
相关标签/搜索