博客
关于我
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 常见问题
查看>>
MYSQL 幻读(Phantom Problem)不可重复读
查看>>
mysql 往字段后面加字符串
查看>>
mysql 快照读 幻读_innodb当前读 与 快照读 and rr级别是否真正避免了幻读
查看>>
MySQL 快速创建千万级测试数据
查看>>
mysql 快速自增假数据, 新增假数据,mysql自增假数据
查看>>
MySql 手动执行主从备份
查看>>
Mysql 批量修改四种方式效率对比(一)
查看>>
mysql 批量插入
查看>>
Mysql 报错 Field 'id' doesn't have a default value
查看>>
MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
查看>>
Mysql 拼接多个字段作为查询条件查询方法
查看>>
mysql 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>