Spring Boot(五):Spring Boot 集成 Spring Security (I)


标签: spring security

2017-12-28 15:33 117人阅读 评论(0) 收藏 举报

 分类:

Springboot8 

版权声明:本文为博主原创文章,欢迎转载指正并注明出处www.onroad.tech

目录(?)[+]

今天我们将在上一篇博客《Spring Boot(四):Spring Boot 集成 Thymeleaf》的基础上来集成SpringSecurity.

1. 添加Maven依赖

在pom.xml引用springsecurity.

<dependency>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-security</artifactId>

</dependency>

添加Maven依赖后,运行项目,访问https://localhost:8443/SpringBootBase/ 浏览器会弹出如下身份验证框:

如图1所示:

这是因为SpringSecurity对我们的工程默认使用”basic”身份认证,只要引入了Spring Security, 那么整个web应用都是安全的,所有资源访问都要经过身份验证授权才可以访问。可用user 及 随机密码登录,随机密码可从web应用启动日志查询, 如:

Using default security password:89c19869-277c-4eba-89c8-590e0405ae84

当然也可以在application.properties里自定义username和password.

security.user.name=admin

security.user.password=123456

或者直接关闭这个默认的basic认证

security.basic.enabled=false

  • 1

详情请参考官方文档:https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/html/boot-features-security.html

当你客制化自己的授权体系后,这个默认的”basic”认证将会自动被替代。

2. 配置spring security

我们还是以上一篇博客的工程代码为基础来整合SpringSecurity.

  • index.html: 登录界面

<!DOCTYPE html>

<htmllang="zh-cn"xmlns:th="http://www.thymeleaf.org">

<head>

    <metacharset="utf-8"/>

 

    <title>Login</title>

 

    <linkrel="stylesheet"th:href="@{css/bootstrap.min.css}"/>

    <linkrel="stylesheet"th:href="@{css/customer/login.css}"/>

</head>

 

<body>

    <divclass="container">

        <h3align="center">这是一个带有登录框的主页</h3>

        <formclass="form-signin"th:action="@{/login}"th:object="${user}"method="post">

            <h2class="form-signin-heading"></h2>

            <inputtype="text"class="form-control"placeholder="账号"th:field="*{username}"/>

            <inputtype="password"class="form-control"placeholder="密码"th:field="*{password}"/>

            <pth:if="${param.logout}"class="error-code">已成功注销</p>

            <pth:if="${param.error}"class="error-code">用户名或者密码错误</p>

            <buttonclass="btn btn-lgbtn-primary btn-block"type="submit">登录</button>

        </form>

    </div>

</body>

</html>

这回我们新增了两行代码:

<pth:if="${param.logout}"class="error-code">已成功注销</p>

<pth:if="${param.error}"class="error-code">用户名或者密码错误</p>

其中th:if=”${param.logout}”为Thymeleaf模板引擎判断语法,表示如果http post/get请求的参数中带有logout,则显示已成功注销。

  • 再增加一个登录成功的欢迎界面welcome.html,带有注销按钮。

<!DOCTYPE html>

<htmlxmlns:th="http://www.thymeleaf.org">

    <head>

        <metacharset="utf-8"/>

        <title>HelloWorld!</title>

    </head>

    <body>

        <h1th:inline="text">Hello[[${#httpServletRequest.remoteUser}]]!</h1>

        <formth:action="@{/logout}"method="post">

            <inputtype="submit"value=""/>

        </form>

    </body>

</html>

th:inline=”text”表示文本内联,即取${#httpServletRequest.remoteUser}值作为文本显示。

  • 在LoginController.java里新增两个controller

@RequestMapping(value ="/welcome",method = RequestMethod.GET)

String welcome() {

  return"welcome";

}

 

@RequestMapping(value ="/login",method = RequestMethod.GET)

String login(Model model, UserVO user) {

  model.addAttribute("user",user);

  return"index";

}

一个是用来welcome跳转,一个是用来login页面跳转。

  • 定制安全策略

接下来写个类WebSecurityConfig来继承WebSecurityConfigurerAdapter,用于确保经过认证的用户才能访问我们设置的需要经过验证的url.

packagetech.onroad.springbootbase.auth;

 

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.context.annotation.Configuration;

importorg.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;

importorg.springframework.security.config.annotation.web.builders.HttpSecurity;

importorg.springframework.security.config.annotation.web.builders.WebSecurity;

importorg.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

 

@Configuration

publicclassWebSecurityConfigextendsWebSecurityConfigurerAdapter{

 

    @Autowired

    publicvoidconfigureGlobal(AuthenticationManagerBuilderauth) throws Exception {

       auth.inMemoryAuthentication()

            .withUser("admin").password("123456").roles("USER");

    }

 

 

    @Override

    protectedvoidconfigure(HttpSecurityhttp) throws Exception {

        http

           .authorizeRequests()

               .antMatchers("/").permitAll()

               .anyRequest().authenticated()

                .and()

           .formLogin()

               .loginPage("/login")

               .defaultSuccessUrl("/welcome")

               .permitAll()

                .and()

            .logout()

               .permitAll();

    }

 

    @Override

    publicvoidconfigure(WebSecurityweb) throws Exception {

        //解决静态资源被拦截的问题

       web.ignoring().antMatchers("/css/**");

    }

}

auth.inMemoryAuthentication()

            .withUser("admin").password("123456").roles("USER");

表示在内存中创建一个username为admin,password为123456,role为USER的用户。但大多数web应用肯定会有自己的用户管理系统,这个我们在接下来的博客会另起一篇博客介绍,这里先用内存用户。

注意:前台login表单传过来的账户名及密码的参数名必须为username和password,否则SpringSecurity无法正确获取用户名及密码与后台用户系统进行匹配,具体原因是Sping Security的默认定义用户系统的用户名和密码为username和password. 如果想详细了解一下为什么,可参考auth.inMemoryAuthentication().withUser(“admin”).password(“123456”).roles(“USER”)源码。

configure(HttpSecurity http)方法是用来定义安全策略,如哪些url路径需要经过授权才能访问,哪些不用。如上面的代码中,”/”就不需要授权就可以访问,即我们可以正常访问https://localhost:8443/SpringBootBase/,而不需要用户授权。

当一个用户成功登录后,即SpringSecurity认证成功后,我们的web应用将重定向到之前用户请求的页面,也可以客制化,使用defaultSuccessUrl方法将其重定向到指定页面。loginPage("/login")表示在没有授权前,任何访问需要授权才能访问的页面都会先跳转到/login登录页面。

web.ignoring().antMatchers("/css/**");表示所以css目录下的静态资源都不作拦截。

3. 验证

按照代码逻辑:我们访问https://localhost:8443/SpringBootBase/,首先会跳转到index.html界面(因为https://localhost:8443/SpringBootBase/https://localhost:8443/SpringBootBase/index.html是一样的),然后输入账户名admin及密码123456,点击登录,将用户名及密码传到后台进行匹配,如果成功,则跳转到welcome.html界面,如果失败,则会默认指向/login?error,而从LoginController,又将其定向到index.html页面,提示用户名或者密码错误。点击welcome界面注销按钮,则会默认跳转到/login?logout,所以又回到index.html页面,显示已成功注销。

我们来运行一下,看结果是不是一样的?

1.      访问https://localhost:8443/SpringBootBase/如图2所示

1.      输入admin及密码123456,点击登录,成功跳转到welcome界面,如图3所示

1.      点击注销登出,如图4所示

1.      输入错误的账户名或密码,则得图5结果

一切如我们预期执行,说明我们springsecurity集成成功了。当然,这只用了spring security最简单的功能,还有自定义用户系统等等,我们接下来会慢慢介绍。


完整代码可到我的github下载
https://github.com/onroadtech/SpringbootBase/ 
branch: master 
commit-id: 2872ee008f23197b5fa28acee95aae378d4d1f01


本博文已同步发表于我的个人博客网站,欢迎转载指正并注明出处。 
个人博客: www.onroad.tech 
指正邮箱: onroad_tech@163.com

 


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值