springsecurity-01-0511
springsecurity-01-0511课堂代码

BaseController
package com.springsecurity.springsecurity.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class BaseController { @RequestMapping({"/","/index"})
public String index(){
return "index";
} @RequestMapping("/tologin")
public String toLogin(){
return "login";
} @RequestMapping("/welcome")
public String toWelcome(){
return "welcome";
} @RequestMapping("/article-list")
public String toArticleList(){
return "article/article-list";
} @RequestMapping("/product-brand")
public String toProductBrand(){
return "product/product-brand";
}
@RequestMapping("/product-category")
public String toProductCategory(){
return "product/product-category"; }
@RequestMapping("/product-list")
public String toProductList(){
return "product/product-list";
} @RequestMapping("/member-list")
public String toMemberList (){
return "member/member-list";
}
@RequestMapping("/member-del")
public String toMemberDel(){
return "member/member-del";
} @RequestMapping("/admin-role")
public String toAdminRole (){
return "admin/admin-role";
}
@RequestMapping("/admin-permission")
public String toAdminPermission(){
return "admin/admin-permission";
}
@RequestMapping("/admin-list")
public String toAdminList(){
return "admin/admin-list";
}
}
SecurityConfig
package com.springsecurity.springsecurity; import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.crypto.bcrypt.BCryptPasswordEncoder; @EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter { // 授权:设置权限(权限划分)和页面(四个菜单项)的访问关联
// admin 页面需要有admin权限
// article页面需要有article权限
// 等等 @Override
protected void configure(HttpSecurity http) throws Exception {
// 关闭springsecurity阻止Frame
http.headers().frameOptions().disable(); // 请求访问需要的权限 http.authorizeRequests().antMatchers("/admin-role","/admin-permission","/admin-list").hasRole("admin")
.antMatchers("/article-*").hasRole("article")
.antMatchers("/member-*").hasRole("member")
.antMatchers("/product-*").hasRole("product"); // 开启登录页面 默认/login springsecurity自带的
// http.formLogin(); // /login提交表单默认的用户名和密码的请求参数是:username和password,还能验证登录失败/login?err,登录成功跳转到首页
http.formLogin().loginPage("/tologin").loginProcessingUrl("/login"); // <from action="/login"> <input name="username"/> <input name=password/>
// 阻止网络攻击的检测----/tologin关联/login出现
http.csrf().disable(); // 开启注销, 默认访问/logout,当用户确认注销跳转到/login?logout登录页面
http.logout(); // 开启记住用户 将用户信息保存在cookie中
http.rememberMe(); } // 认证:用户和权限绑定
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// 用户持久化:数据库 内存
// 101用户对应的权限article
// SpringSecurity5.0 版本对于密码进行不同规则的加密
// passwordEncoder()
// BCryptPasswordEncoder加密格式对密码进行加密 auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("101").password(new BCryptPasswordEncoder().encode("1")).roles("article").and()
.withUser("102").password(new BCryptPasswordEncoder().encode("1")).roles("product").and()
.withUser("103").password(new BCryptPasswordEncoder().encode("1")).roles("member").and()
.withUser("104").password(new BCryptPasswordEncoder().encode("1")).roles("admin");
} }
springsecurity-01-0511的更多相关文章
- 用SpringSecurity从零搭建pc项目-01
注:之前写过一些列的SpringSecurity的文章,重新写一遍是为了把某些不必要的步骤省去,留下精简的,因为工作中有一些不需要. 在java的权限框架里,shiro和SpringSecurity是 ...
- SpringMVC+spring-security+sitemesh+hibernate+freemarker整合-转
http://www.oschina.net/code/snippet_170632_46774 代码分享 当前位置: 代码分享 » Java » Web编程 搜 索 SpringMVC+spr ...
- SpringSecurity的简单应用(一)
java项目首先要提的就是jar包了,Springsecurity的jar下载地址:http://static.springsource.org/spring-security/site/downlo ...
- web项目学习之spring-security
转自<http://liukai.iteye.com/blog/982088> spring security功能点总结: 1. 登录控制 2. 权限控制(用户菜单的显示,功能点访问控制) ...
- SpringBoot19 集成SpringSecurity01 -> 环境搭建、SpringSecurity验证
1 环境搭建 1.1 创建一个SpringBoot项目 项目脚手架 -> 点击前往 1.2 创建一个Restful接口 新建一个Controller类即可 package com.example ...
- SpringBoot整合SpringSecurity简单实现登入登出从零搭建
技术栈 : SpringBoot + SpringSecurity + jpa + freemark ,完整项目地址 : https://github.com/EalenXie/spring-secu ...
- Spring Cloud 之Spring-Security
对于Spring-Security首先要明白这么几点: 1.什么是SpringSecurityurity2.SpringSecurity应用场景3.SpringBoot整合Security4.Secu ...
- SpringMVC+springSecurity+flexPaper 配置--类似百度文库在线预览
背景:现在项目需要做一个类似百度文库的在线预览功能,在网上找了下很多人推荐使用FlexPaper,所以今天尝试学习了FlexPaper顺便集成到现有的框架中 由于网上目前的说的都不是很详细,所以现在记 ...
- SpringBoot20 集成SpringSecurity02 -> 利用SpringSecurity进行前后端分离的登录验证
1 SpirngBoot环境搭建 创建一个SpringBoot项目即可,详情参见三少的相关博文 参考博文 -> 点击前往 SpirngBoot项目脚手架 -> 点击前往 2 引入Spirn ...
- SpringSecurity学习四----------基于不同角色跳转到不同URL
© 版权声明:本文为博主原创文章,转载请注明出处 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0& ...
随机推荐
- LNMP架构——源码编译安装
LNMP架构--源码编译安装 1.编译安装nginx服务 2.编译安装mysql服务 3.编译安装php解析环境 1.编译安装nginx服务: systemctl stop firewalld sys ...
- 2022寒假集训day5
day5 五道栈的题加上字符串. 单调队列. T1 表达式括号匹配 洛谷P1739 题目描述 假设一个表达式有英文字母(小写).运算符(+,-,*,/)和左右小(圆)括号构成,以"@&q ...
- 用JavaScript+HTML实现双色球随机摇号效果
用JavaScript+HTML实现双色球随机摇号效果 首先要知道双色球的规则是什么 双色球投注区分为红球号码区和蓝球号码区 红球号码范围为01-33,蓝球号码范围为01-16 双色球每期从33个红球 ...
- suse 12 部署chrony时间同步服务器
文章目录 1.ntp和chrony的区别 1.1.关于chrony 1.2.chronyd的优势 2.环境介绍 3.部署chrony 4.配置chrony 4.1.配置文件解析 4.2.查看chron ...
- matlab文件处理
1.读取文件(按行读取) fid = open('file_name');while(~feof(fid)) line = fgetl(fid); % 读取一行数据 endfid.close(); 2 ...
- JavaScript函数式编程(纯函数、柯里化以及组合函数)
JavaScript函数式编程(纯函数.柯里化以及组合函数) 前言 函数式编程(Functional Programming),又称为泛函编程,是一种编程范式.早在很久以前就提出了函数式编程这个概念了 ...
- Devops 开发运维高级篇之微服务代码上传和代码检查
Devops 开发运维高级篇之微服务代码上传和代码检查 微服务持续集成(1)-项目代码上传到Gitlab 微服务持续集成(2)-从Gitlab拉取项目源码 微服务持续集成(3)-提交到SonarQub ...
- jdk、jre、jvm分别是什么?有什么联系?
JDK:是Java Development Kit的缩写,是Java的开发工具包,JDK是整个JAVA的核心.它提供了编译.运行Java程序所需的各种工具和资源.有了它,Java开发者就可以编译和运行 ...
- netty系列之:channelHandlerContext详解
目录 简介 ChannelHandlerContext和它的应用 AbstractChannelHandlerContext DefaultChannelHandlerContext 总结 简介 我们 ...
- Oracle的发展历程
我们学习的是ORACLE(甲骨文)公司(就是收购Sun公司的甲骨文公司)的Oracle数据库(Oracle Database).Oracle数据库是关系型数据库中的大型数据库,存储量大,而且也非常安全 ...