博客
关于我
Spring security安全框架的使用
阅读量:598 次
发布时间:2019-03-12

本文共 6340 字,大约阅读时间需要 21 分钟。

入门案例

创建一个maven工程

第一步导入pom.xml

4.0.0
com.offcn
spring_security_demo
1.0-SNAPSHOT
war
3.0
4.2.5.RELEASE
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework.security
spring-security-web
4.1.0.RELEASE
org.springframework.security
spring-security-config
4.1.0.RELEASE
javax.servlet
servlet-api
2.5
provided
maven-compiler-plugin
1.8
1.8
org.apache.tomcat.maven
tomcat7-maven-plugin
9090
/

框架所必须的依赖

org.springframework.security
spring-security-web
4.1.0.RELEASE
org.springframework.security
spring-security-config
4.1.0.RELEASE

第二步配置web.xml

contextConfigLocation
classpath:spring/application_security.xml
org.springframework.web.context.ContextLoaderListener
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*

第三步写application_security.xml配置文件

第四步写页面

login.html

注意:1.用户名name必须是username 2.密码name必须是possword 3.提交地址必须是/login 4.提交方式必须是post

用户名:
密码

成功页面还要失败页面就不写了

将Spring security融入项目中的操作

第一步在web.xml中添加一个过滤器

springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*

第二步写application_security.xml配置文件

第三步 修改登陆表单的action还要属性名等

注意:登陆访问的接口是/login 注销操作访问的接口是/logout

注销举例:

注销
内容补充:当你登陆成功后还要获取登陆名信息的话,再写一个接口
@RestController@RequestMapping("/login")public class LongController {       @RequestMapping("/name")    public Map name(){           //Context上下文        String name = SecurityContextHolder.getContext().getAuthentication().getName();        Map map = new HashMap();        map.put("loginName",name);        return map;    }}

自定义认证类

1.写一个类实现UserDetailsService接口

package com.offcn.service;import org.springframework.security.core.GrantedAuthority;import org.springframework.security.core.authority.SimpleGrantedAuthority;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.core.userdetails.UsernameNotFoundException;import java.util.ArrayList;import java.util.List;public class UserDetailsServiceImpl implements UserDetailsService {       //UserDetails 认证的一个接口 有一个实现类 User    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {          //自定义一个安全的集合  ROLE_TEACGER 是配置拥有访问权限的用户的角色    GrantedAuthority:权限        List
Granted = new ArrayList
(); //角色名必须是配置文件中已经配置的角色 Granted.add(new SimpleGrantedAuthority("ROLE_SELLER")); return new User(username,"123",Granted); //这里写username 表示用户名可以任意 密码是123 就可以通过验证 }}

2.修改spring-security.xml配置文件

只修改认证管理器

转载地址:http://tobxz.baihongyu.com/

你可能感兴趣的文章
MySQL5.6的zip包安装教程
查看>>
mysql5.7 for windows_MySQL 5.7 for Windows 解压缩版配置安装
查看>>
Webpack 基本环境搭建
查看>>
mysql5.7 安装版 表不能输入汉字解决方案
查看>>
MySQL5.7.18主从复制搭建(一主一从)
查看>>
MySQL5.7.19-win64安装启动
查看>>
mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
查看>>
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>