博客
关于我
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/

你可能感兴趣的文章
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
mui折叠面板点击事件跳转
查看>>
MySQL 8 公用表表达式(CTE)—— WITH关键字深入用法
查看>>
mysql 8 远程方位_mysql 8 远程连接注意事项
查看>>
MUI框架里的ajax的三种方法
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
Mysql 8.0 新特性
查看>>
MultCloud – 支持数据互传的网盘管理
查看>>
MySQL 8.0.23中复制架构从节点自动故障转移
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>