网站 微信,南宁市网站,互联网推广专员是做什么的,wordpress安装后403我们在编写Web应用时#xff0c;经常需要对页面做一些安全控制#xff0c;比如#xff1a;对于没有访问权限的用户需要转到登录表单页面。要实现访问控制的方法多种多样#xff0c;可以通过Aop、拦截器实现#xff0c;也可以通过框架实现#xff08;如#xff1a;Apache…我们在编写Web应用时经常需要对页面做一些安全控制比如对于没有访问权限的用户需要转到登录表单页面。要实现访问控制的方法多种多样可以通过Aop、拦截器实现也可以通过框架实现如Apache Shiro、Spring Security。
本文将具体介绍在Spring Boot中如何使用Spring Security进行安全控制。
准备工作
首先构建一个简单的Web工程以用于后续添加安全控制也可以用之前Chapter3-1-2做为基础工程。若对如何使用Spring Boot构建Web应用可以先阅读《Spring Boot开发Web应用》一文。
Web层实现请求映射
Controllerpublic class HelloController { RequestMapping(/) public String index() { return index; } RequestMapping(/hello) public String hello() { return hello; }}/映射到index.html/hello映射到hello.html
实现映射的页面
src/main/resources/templates/index.html
!DOCTYPE htmlhtml xmlnshttp://www.w3.org/1999/xhtml xmlns:thhttp://www.thymeleaf.org xmlns:sechttp://www.thymeleaf.org/thymeleaf-extras-springsecurity3 head titleSpring Security入门/title /head body h1欢迎使用Spring Security!/h1 p点击 a th:href{/hello}这里/a 打个招呼吧/p /body/htmlsrc/main/resources/templates/hello.html
!DOCTYPE htmlhtml xmlnshttp://www.w3.org/1999/xhtml xmlns:thhttp://www.thymeleaf.org xmlns:sechttp://www.thymeleaf.org/thymeleaf-extras-springsecurity3 head titleHello World!/title /head body h1Hello world!/h1 /body/html可以看到在index.html中提供到/hello的链接显然在这里没有任何安全控制所以点击链接后就可以直接跳转到hello.html页面。
整合Spring Security
在这一节我们将对/hello页面进行权限控制必须是授权用户才能访问。当没有权限的用户访问后跳转到登录页面。
添加依赖
在pom.xml中添加如下配置引入对Spring Security的依赖。
dependencies ... dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-security/artifactId /dependency .../dependenciesSpring Security配置
创建Spring Security的配置类WebSecurityConfig具体如下
ConfigurationEnableWebSecuritypublic 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
Controllerpublic class HelloController { // 省略之前的内容... RequestMapping(/login) public String login() { return login; }}新增登录页面src/main/resources/templates/login.html
!DOCTYPE htmlhtml xmlnshttp://www.w3.org/1999/xhtml xmlns:thhttp://www.thymeleaf.org xmlns:sechttp://www.thymeleaf.org/thymeleaf-extras-springsecurity3 head titleSpring Security Example /title /head body div th:if${param.error} 用户名或密码错 /div div th:if${param.logout} 您已注销成功 /div form th:action{/login} methodpost divlabel 用户名 : input typetext nameusername/ /label/div divlabel 密 码 : input typepassword namepassword/ /label/div divinput typesubmit value登录//div /form /body/html可以看到实现了一个简单的通过用户名和密码提交到/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让它输出一些内容并提供“注销”的链接。
!DOCTYPE htmlhtml xmlnshttp://www.w3.org/1999/xhtml xmlns:thhttp://www.thymeleaf.org xmlns:sechttp://www.thymeleaf.org/thymeleaf-extras-springsecurity3 head titleHello World!/title /head body h1 th:inlinetextHello [[${#httpServletRequest.remoteUser}]]!/h1 form th:action{/logout} methodpost input typesubmit value注销/ /form /body/html本文通过一个最简单的示例完成了对Web应用的安全控制Spring Security提供的功能还远不止于此更多Spring Security的使用可参见Spring Security Reference。
代码示例
本文的相关例子可以查看下面仓库中的chapter4-3-1目录
Githubhttps://github.com/dyc87112/SpringBoot-LearningGiteehttps://gitee.com/didispace/SpringBoot-Learning
如果您觉得本文不错欢迎Star支持您的关注是我坚持的动力