在这一节,我们将把之前的SeurityConfiguration的类分成下面两个:

1)        ApiSecurityConfiguration :这个是首先被布局的。这个确保使用基本的身份验证RESTful。

2)        WebSecurityConfiguration :这个是布局重置登录的页面。

1.  ApiSecurityConfiguration

我们一样的删除了之前的SeurityConfiguration,然后在相同的包下面添加ApiSecurityConfiguration类,加入的代码如下 :

package masterSpringMVC6.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/*** 基础权限的设定,关于用户的权限* Created by OwenWilliam on 2016/5/21.*/@Configuration
@Order(1)//最先执行
public class ApiSecurityConfiguration extends WebSecurityConfigurerAdapter
{/*** 这个是数据库提取相匹配的。* 这里我们规定是固定的用户名和密码* @param auth* @throws Exception*/@Autowiredpublic void configureAuth(AuthenticationManagerBuilder auth)  throws Exception{auth.inMemoryAuthentication().withUser("user").password("user").roles("USER").and().withUser("admin").password("admin").roles("USER", "ADMIN");}/*** 规定什么样的角色可以有什么样的操作* 如,User的只能查看,admin有CRUD* @param http* @throws Exception*/@Overrideprotected void configure(HttpSecurity http) throws Exception {http.antMatcher("/api/**").httpBasic().and().csrf().disable().authorizeRequests().antMatchers(HttpMethod.GET).hasRole("USER").antMatchers(HttpMethod.POST).hasRole("ADMIN").antMatchers(HttpMethod.PUT).hasRole("ADMIN").antMatchers(HttpMethod.DELETE).hasRole("ADMIN").anyRequest().authenticated();}
}

2. WebSecurityConfiguration

在相同的包下添加,然后加下的代码如下:

package masterSpringMVC6.config;import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/*** 登录的权限,像URI的地址* Created by OwenWilliam on 2016/5/21.*/@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter
{@Overrideprotected void configure(HttpSecurity http) throws Exception {http.formLogin().loginPage("/login").defaultSuccessUrl("/profile").and().logout().logoutSuccessUrl("/login").and().authorizeRequests().antMatchers("/webjars/**", "/login", "/signin/**", "/signup").permitAll().anyRequest().authenticated();}
}

上面的代码就已经规格了登录的页面的重置到login的视图页面。所以我们还需要加入个视图页面。在加入视图页面时,我们需要定义个控制器,让控制器来调用视图。

在authentication的包下加入LoginController的类。

package masterSpringMVC6.authentication;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;/*** 用户普通登录* Created by OwenWilliam on 2016/5/21.*/@Controller
public class LoginController {@RequestMapping("/login")public String authenticate(){return "login";}
}

3.视图界面

好了,我们现在添加个login.html.

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org"xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"layout:decorator="layout/default">
<head><title>Login</title>
</head>
<body>
<div class="section no-pad-bot" layout:fragment="content"><div class="container"><h2 class="header center orange-text">Login</h2><div class="row"><div id="errorMessage" class="card-panel red lighten-2" th:if="${param.error}"><span class="card-title">Invalid user name or password</span></div><form class="col s12" action="/login" method="post"><div class="row"><div class="input-field col s12"><input id="username" name="username" type="text" class="validate"/><label for="username">Username</label></div></div><div class="row"><div class="input-field col s12"><input id="password" name="password" type="password" class="validate"/><label for="password">Password</label></div></div><div class="row center"><button class="btn indigo waves-effect waves-light" type="submit" name="action">Submit<i class="mdi-content-send right"></i></button></div><!--伪造自己的信息,使其可信用,去跨站访问--><input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/></form></div></div>
</div>
</body>
</html>

4.总结

这章节的学习,我们主要是优化上一节的内容,我们自己定义一个登录的页面,这个页面更适合我们的系统。同时,我们将SeurityConfiguration给分割成了两个,可以说ApiSeurityConfiguration是API用的,而WebSeurityConfiguration是我们访问网站时的页面跳转。最后,我们登录的页面如下。

源码下载:git@github.com:owenwilliam/masterSpringMVC.git

SpringMVC之安全性(二)登录界面相关推荐

  1. ASP.net 简单登录界面

    一.说明 此文是小白在学习张晨光老师的视频教学<<Asp.Net WEB服务器编程技术>>中做的学习笔记,一些知识点也是跟着教程走的,大家也可以去老师的主页去学习,谢谢大家. ...

  2. androidstudio做登录界面_Vue-cli+Element-ui实现后台管理系统(二)实现后台登录功能...

    前言 接上文,本文主要讲解vue+element-ui后台管理系统的登录功能的实现,api接口这块如果对后端技术以及node的实现不太了解的情况下,可以写出假数据进行模拟操作~ 一.创建登录文件并配置 ...

  3. PyQt5教程(八)——实现QQ登录界面(二、加载资源文件)

                                         实现QQ登录界面--加载资源文件 一.创建资源文件: 上篇文章创建了QQ登录界面,本篇介绍创建并加载资源文件. 1.创建资源文 ...

  4. 【HTML界面设计(二)】说说模块、登录界面

    记录很早之前写的前端界面(具体时间有点久远) 一.说说模板 采用 适配器(Adapter)原理 来设计这款说说模板,首先看一下完整效果 这是demo样图,需要通过业务需求进行修改的部分 这一部分,就是 ...

  5. Swing学习----------QQ登录界面制作(二)

    素材获取可关注微信公众号:开源IT,后台回复 "素材" 免费获取. 上次把QQ界面的主题框架实现了,但是还没有完成最小化,关闭按钮等的功能,这次重新完善了QQ登录界面的功能,本次更 ...

  6. PyQt5教程(十二)——实现QQ登录界面(六、实现鼠标拖动界面,鼠标事件)

    PyQt5教程(十二)--实现QQ登录界面(五.实现鼠标拖动界面,鼠标事件) 一.实现界面可以随着鼠标进行拖动 1.主要就是对鼠标事件的实现: def mousePressEvent(self, ev ...

  7. 【Qt Quick聊天软件练习】二、登录界面搭建

    目录 1 主界面 2 创建登录面板qml文件 3 补充 ✨结语✨ 1 主界面 大概长成这样 2 创建登录面板qml文件 新建一个qml文件命名为LoginPanel.qml,首先先把右上角两个圆圈搓出 ...

  8. .net学习笔记——学生信息管理系统(二、windows窗体实现登录界面)

    (学习目标:使用.net 窗体制作一个学生信息管理系统,满足学生信息.班级信息.年级信息的增删改查.) 第二天:登录界面的实现 任务:在Microsoft Visual Studio中新建一个Wind ...

  9. 请你根据微信登录界面设计测试用例

    请你根据微信登录界面设计测试用例 参考回答: 一.功能测试 1.输入正确的用户名和密码,点击提交按钮,验证是否能正确登录. 2.输入错误的用户名或者密码,验证登录会失败,并且提示相应的错误信息. 3. ...

  10. 浅析网站 APP 登录界面设计

    无论网页或是移动APP的设计,很重要的一点是如何能在小而美和功能复杂性之间找到平衡点.本文就移动APP表单设计进行浅析,看设计师是如何在设计与交互体验之间做到小而美的平衡. 一.极致的减法 这是一个异 ...

最新文章

  1. IDEA实用插件和技巧
  2. 【网络流24题】解题报告:A、飞行员配对方案问题(最大流求二分图最大匹配)
  3. 靠二进制画几何[图论]
  4. 在NumericStepper控件中使用嵌入字体显示数字.
  5. OSPF 多进程实验(1)
  6. java fx 内置图标_图标 – 如何在Windows上为javafx本机程序包图标设置自定义图标...
  7. visual studio 2015 配置好qt5后, 第一次运行出现 无法打开源文件“QtWidgets/QApplication”和无法运行rc.exe的解决方案
  8. 开源供应链金融_成为开源供应链
  9. RefreshLayout刷新组件,有详细注释适合使用和中高端学习
  10. Stanford机器学习---第一讲. Linear Regression with one variable
  11. 十二、K8s job cronjob相关操作
  12. ansys17.0安装教程
  13. 一文读懂YUV的采样与格式
  14. 域控-笔记四(综合应用)
  15. python:批量修改文件夹名称
  16. 计算机毕业设计Java高原特色农产品网站设计(源码+系统+mysql数据库+Lw文档)
  17. 《和沐阳一起学POST+JS逆向》视频教程
  18. 仅需三步学会使用低代码ThingJS与森数据DIX数据对接
  19. Unity项目-黑魂复刻(四)玩家控制器(翻滚以及跳跃操作改动)
  20. C++技术的主要应用领域

热门文章

  1. 前端页面——Cookie与Session有什么差别
  2. VBscript读取excel表,中数据导入到SQLserver数据库里面
  3. C语言 · 冒泡法排序
  4. NHibernate查询语言(HQL)
  5. 自制有保鲜功能的金字塔
  6. 游戏筑基开发之动态数组(C语言)
  7. OSPF区域间路由计算规则与实验
  8. LNMP详解(六)——Nginx location语法详解
  9. 杭电计算机2013年硕士研究生复试详解
  10. HDOJ--2066--一个人的旅行