请先查看 初识Spring security-无Security的SpringMVC

在pom.xml文件中添加包

<!-- Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.version}</version>
</dependency>
在web.xml文件中添加配置
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在HelloController文件中添加代码
@RequestMapping(value = "/admin**", method = RequestMethod.GET)
public ModelAndView adminPage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "Spring Security Hello World");
model.addObject("message", "This is protected page!);
model.setViewName("admin");
return model;
}

添加模板文件admin.html,内容与hello.html一致

关键配置在spring-security.xml文件中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<security:http auto-config="true">
<security:intercept-url pattern="/admin**" access="hasRole('ROLE_USER')"/>
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="hongxf" password="123456" authorities="ROLE_USER" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
表明系统中添加一个用户名是hongxf,密码是123456,角色是ROLE_USER的用户,并配置访问/admin路径及其子路径必须含有角色ROLE_USER。
启动应用,访问http://localhost:8080/admin,则会跳转到http://localhost:8080/login,默认登录页

 输入错误的用户名和密码,则会显示
输入正确的用户名和密码则会跳转到admin.html页面,说明简单的权限控制已经完成

 

初识Spring security-添加security的更多相关文章

  1. 初识Spring Security

    本文参考或者转自:http://haohaoxuexi.iteye.com/blog/2154299 1.新建Spring Security配置文件spring-security.xml:<?x ...

  2. 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置

    在上篇博客初识Spring Boot框架中我们初步见识了SpringBoot的方便之处,很多小伙伴可能也会好奇这个Spring Boot是怎么实现自动配置的,那么今天我就带小伙伴我们自己来实现一个简单 ...

  3. 初识Spring

    Spring简介 说到Spring就得提到它的作者"Rod Johnson",2002年他编写了<Expert One-on_One java EE设计与开发>一书.在 ...

  4. Spring中添加新的配置表,并对新的配置表进行处理

    实习过程中boss交代的任务(以下出现的代码以及数据只给出小部分,提供一个思路) 目的:Spring中添加新的配置表,并对新的配置表进行处理:替换的新的配置表要友好,同时保证替换前后功能不能发生变化. ...

  5. 初识Identity并添加身份验证管理页面

    目录 初识Identity并添加身份验证管理页面 前言 什么是ASP.NET Core Identity 创建带有身份验证的WebApp 尝试运行 检查解决方案中的项目文件 发现问题 原因 解决问题 ...

  6. Spring Boot 添加JSP支持【转】

    Spring Boot 添加JSP支持 大体步骤: (1)            创建Maven web project: (2)            在pom.xml文件添加依赖: (3)     ...

  7. 初识 Spring 框架

    初识 Spring 框架可以帮助我们构建规范的.优秀的应用程序,简化烦琐的编码过程. Spring 是一个非常著名的轻量级的企业级开源框架,Spring 的目标是使 Java EE 更易用并促进良好的 ...

  8. SpringBoot系列之Spring容器添加组件方式

    SpringBoot系列之Spring容器添加组件方式 本博客介绍SpringBoot项目中将组件添加到Spring容器中的方法,SpringBoot项目有一个很明显的优点,就是不需要再编写xml配置 ...

  9. 【初识Spring】对象(Bean)实例化及属性注入(xml方式)

    title: [初识Spring]对象(Bean)实例化及属性注入(xml方式) date: 2018-08-29 17:35:15 tags: [Java,Web,Spring] --- #初识S ...

随机推荐

  1. day6笔记

    一.上节回顾 list:li = [1,2,3,5,'a']增加:append:末尾加入==追加 insert:插入,在任意位置,insert(index,'内容') extend:迭代着加入,'as ...

  2. 解决erlang R17无法识别中文问题

    erlang更新到R17已有一段时间了.公司项目打算从旧版的erlang迁移到R17,却不料有不少的困扰,当中一个问题是中文问题. 这个问题非常easy重现:新建一个文件t.erl.保存为utf-8无 ...

  3. C# 关于在原图中寻找子图片坐标的类

    在网上找了好久,没有一个现成的例子,自己也发帖子可惜没有找到好办法. 只好自己动手写了, 以下为个人想法所写,算法可能不会太好,如果各位有好的例子发来大家一起分享一下. 这个类主要实现了图片坐标查找功 ...

  4. jQuery之map()和get() map().get().join意思

    jQuery下有个概念叫“类数组”,比如$( " li " ),当取到一个集合的时候,会有数组的一些属性,但是instancseof Array仍然是false.但是var a=$ ...

  5. ES6学习笔记(一)——let和const

    1.ES6学习之let.const (1).var.let.const 变(常)量声明 ES5 只有全局作用域和函数作用域,没有块级作用域,这带来很多不合理的场景. 在ES6中let就诞生了,实际上它 ...

  6. lodash的使用

    Lodash是一个一致性.模块化.高性能的 JavaScript 实用工具库,内部封装了很多字符串.数组.对象等常见数据类型的处理函数. 为什么选择 Lodash ? Lodash 通过降低 arra ...

  7. 人工智能-baidu-aip语音识别(语音转文字)

    做这个之前,需要在电脑上安装FFmpeg工具,将要转的语音格式转为PCM格式.FFmpeg不需要安装,下载后,打开bin文件夹,然后将路径放在系统环境变量里.记住,要关闭所有打开的Pycharm,然后 ...

  8. Linux中的环境变量配置文件及其作用

    登录相关的配置文件: /etc/profile 范围:对所有用户生效 作用: a.定义USER变量 b.定义LOGNAME变量 c.定义MAIL变量 d.定义PATH变量 e.定义HOSTNAME变量 ...

  9. Dubbo学习和配置(转载)

    转载自: 简单了解下Dubbo 1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架, ...

  10. Sql多条件排序

    多条件排序可以通过在order by语句后面使用case when then条件语句来实现. end 例子: 1.创建表case_test 共有id,case_type,case_location,c ...