Spring Guide:Securing a Web Application(中文大概意思)
这个教程我已经看过很多次,无奈根基太薄弱,只能是照着做出结果,很多未能领会。不过我感觉基础还是servlet and JSP.
我的IDE环境该升级了,点击SpringToolsSuiteIDE环境中Help-->Check for Update 可以升级,我的好像是431Mb的下载量,一会就升级成功了。
如果不不升级你会发现在https://start.spring.io/ 向导创建的下面解压后也导入进入IDE环境会出错,不让你导入。
Securing a Web Application
Starting with Spring Initializr
去https://start.spring.io/创建新的应用程序, Artifact中是:securing-web,右侧依赖选择Spring Web 和Thymeleaf 。点击Genreate 下载压缩包,解压缩。
Spring ToolsSute中打开 该项目
Create an Unsecured Web Application
创建一个不安全的Web应用。就是不加安全限制的,Web应用。src/main/resources/templates/home.html目录下创建home.html 文件:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3"><head><title>Spring Security Example</title></head><body><h1>Welcome!</h1><p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p></body>
</html>
还有hello.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3"><head><title>Hello World!</title></head><body><h1>Hello world!</h1></body>
</html>
添加src/main/java/com/example/securingweb/MvcConfig.java 类:
package com.example.securingweb;import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class MvcConfig implements WebMvcConfigurer {public void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/home").setViewName("home");registry.addViewController("/").setViewName("home");registry.addViewController("/hello").setViewName("hello");registry.addViewController("/login").setViewName("login");}}
至此 不带安全的应用就建设好了。IDE中 ,右键--运行--SpringBootApp
之后浏览器中输入:http://localhost:8080/
点击Here 出现:
下面是添加安全
只要是Spring Security is on the classpath, 我理解是pom文件中有SpringSecurity了,那么Spring Boot automatically secures all HTTP endpoints with “basic” authentication.那么SB就给你做基础的认证。
如果想做更多的安全功能,那么添加在pom.xml文件中加入两个条目。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-test</artifactId><scope>test</scope>
</dependency>
src/main/java/com/example/securingweb/WebSecurityConfig.java中加入该类:
package com.example.securingweb;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/", "/home").permitAll().anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll().and().logout().permitAll();}@Bean@Overridepublic UserDetailsService userDetailsService() {UserDetails user =User.withDefaultPasswordEncoder().username("user").password("password").roles("USER").build();return new InMemoryUserDetailsManager(user);}
}
src/main/resources/templates/login.html 添加login.html页面:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"xmlns:sec="https://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>
修改hello.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3"><head><title>Hello World!</title></head><body><h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1><form th:action="@{/logout}" method="post"><input type="submit" value="Sign Out"/></form></body>
</html>
这样就是hello用户名了。
运行即可
效果如下:
现在点击here 出现:
输入用户名:user 密码:password 点击 SignIn 出现:
点击SignOut 出现:
如果用户名密码错误,出现:
完了,完了,完了。
Spring Guide:Securing a Web Application(中文大概意思)相关推荐
- Spring Boot文档阅读笔记-对Securing a Web Application解析
首先创建一个非安全的Web应用 这个应用包含两个页面,一个是home页面,一个是"Hello,World"页面.home页面使用Thymeleaf,相关代码如下: <!DOC ...
- 8.1.4 Authentication in a Web Application
8.1.4 Authentication in a Web Application Now let's explore the situation where you are using Spring ...
- Understanding Spring Web Application Architecture: The Classic Way--转载
原文地址:http://www.petrikainulainen.net/software-development/design/understanding-spring-web-applicatio ...
- struts2官方 中文教程 系列一:创建一个struts2 web Application
先贴了本帖地址,以免被爬 http://www.cnblogs.com/linghaoxinpian/p/6898779.html 本教程将会通过安装struts2框架来创建一个简单的应用程序. 虽 ...
- Spring - Java/J2EE Application Framework 应用框架 第 16 章 通过Spring使用远程访问和web服务
第 16 章 通过Spring使用远程访问和web服务 16.1. 简介 Spring提供类用于集成各种远程访问技术.这种对远程访问的支持可以降低你在用POJO实现支持远程访问业务时的开发难度.目前, ...
- Spring Boot(二):Web 综合开发
Spring Boot(二):Web 综合开发 上篇文章介绍了 Spring Boot 初级教程:Spring Boot(一):入门篇,方便大家快速入门.了解实践 Spring Boot 特性:本篇文 ...
- 2020-4-24 Open Web Application Security Project (OWASP)
英文 中文 The Open Web Application Security Project (OWASP) is a nonprofit foundation that works to impr ...
- Web Scalability for Startup Engineers TipTechniques for Scaling You Web Application --读书笔记
Web Scalability for Startup Engineers Tip&Techniques for Scaling You Web Application 第1章和第2章讲述可伸 ...
- (转)VS2005 SP1发布,解决只能创建WebSite,无法创建Web Application项目的问题
微软的Visual Studio 2005 Service Pack 1 (SP1) 年前就发布了, 年前终于有点时间了,于是装了一下VS2005 sp1,看看到底有什么好东西.这次发布的语言版本包括 ...
- WEB Application Development Integrator : 应用设置
2.1. 系统安装 应用 Oracle EBS WEB Application Development Integrator WEB ADI在Oracle EBS 11.5.10.* 版本 ...
最新文章
- 【代码真相】函数调用 堆栈
- 专业写博一天------ArrayList 线程安全
- Linux和Windows栈帧机器码,栈溢出原理与 shellcode 开发
- python3精要(33)-字典解析与集合解析,if else 用于解析
- 解决 GTK+/GNOME 3 环境下 Java Swing 程序使用本地 GTK+ 主题时菜单无边框 bug 的方法...
- 【渝粤教育】国家开放大学2018年春季 7394-22T政府公共关系 参考试题
- 160 - 38 CyberBlade.2
- Eureka Client注册到Eureka Server的秘密
- bzoj 1596 电话网络
- 自动化运维-Ansible (第三部:Playbook 介绍)
- 华硕CSM不能设置解决方案
- RK 3568 IDB烧录失败解决方法
- 菜鸟入门_Python_机器学习(1)_线性可分的双月实验
- RPG游戏-任务系统
- token过期后刷新token并重新发起请求
- 元旦到了,手把手教你用 Python 制作一个炫酷烟花秀
- js一维数组,api,二维数组
- 安装python2.7安装方法_python2.7环境如何安装
- 等待我们终将飞翔的那天—兄弟连IT教育
- UAP 添加字典表