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

你可能感兴趣的文章
MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
查看>>
MySQL中的GROUP_CONCAT()函数详解与实战应用
查看>>
MySQL中的IO问题分析与优化
查看>>
MySQL中的ON DUPLICATE KEY UPDATE详解与应用
查看>>
mysql中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
查看>>
mysql中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
查看>>
mysql中的字段如何选择合适的数据类型呢?
查看>>
MySQL中的字符集陷阱:为何避免使用UTF-8
查看>>
mysql中的数据导入与导出
查看>>
MySQL中的时间函数
查看>>
mysql中的约束
查看>>
MySQL中的表是什么?
查看>>
mysql中穿件函数时候delimiter的用法
查看>>
Mysql中索引的分类、增删改查与存储引擎对应关系
查看>>
Mysql中索引的最左前缀原则图文剖析(全)
查看>>
MySql中给视图添加注释怎么添加_默认不支持_可以这样取巧---MySql工作笔记002
查看>>
Mysql中获取所有表名以及表名带时间字符串使用BetweenAnd筛选区间范围
查看>>