资讯专栏INFORMATION COLUMN

spring系列---Security 安全框架使用和文件上传FastDFS

K_B_Z / 2645人阅读

摘要:框架入门简介是一个能够为基于的企业应用系统提供声明式的安全访问控制解决方案的安全框架。

1.Spring Security框架入门 1.1 Spring Security简介

</>复制代码

  1. Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。
1.2 Spring Security入门小Demo 1.2.1最简单Demo

</>复制代码

  1. (1)创建工程spring_security_demo ,pom.xml内容

</>复制代码

  1. 4.0.0
  2. cn.itcast.demo
  3. spring-security-demo
  4. war
  5. 0.0.1-SNAPSHOT
  6. 4.2.4.RELEASE
  7. org.springframework
  8. spring-core
  9. ${spring.version}
  10. org.springframework
  11. spring-web
  12. ${spring.version}
  13. org.springframework
  14. spring-webmvc
  15. ${spring.version}
  16. org.springframework
  17. spring-context-support
  18. ${spring.version}
  19. org.springframework
  20. spring-test
  21. ${spring.version}
  22. org.springframework
  23. spring-jdbc
  24. ${spring.version}
  25. org.springframework.security
  26. spring-security-web
  27. 4.1.0.RELEASE
  28. org.springframework.security
  29. spring-security-config
  30. 4.1.0.RELEASE
  31. javax.servlet
  32. servlet-api
  33. 2.5
  34. provided
  35. org.apache.maven.plugins
  36. maven-compiler-plugin
  37. 3.2
  38. 1.7
  39. 1.7
  40. UTF-8
  41. org.apache.tomcat.maven
  42. tomcat7-maven-plugin
  43. 9090
  44. /

</>复制代码

  1. (2)创建web.xml

</>复制代码

  1. contextConfigLocation
  2. classpath:spring-security.xml
  3. org.springframework.web.context.ContextLoaderListener
  4. springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy
  5. springSecurityFilterChain
  6. /*

</>复制代码

  1. (3)创建index.html 内容略(IDEAindex.jsp也可用)
  2. (4)创建spring 配置文件spring-security.xml

</>复制代码

</>复制代码

  1. **配置说明**
  2. intercept-url 表示拦截页面
  3. // 表示的是该目录下的资源,只包括本级目录不包括下级目录
  4. // 表示的是该目录以及该目录下所有级别子目录的资源
  5. form-login 为开启表单登陆

use-expressions 为是否使用使用 Spring 表达式语言( SpEL ),默认为true ,如果开启,则拦截的配置写成以下形式

</>复制代码

此时启动localhost:9090就能看到登陆页面

2项目中的配置及使用

pom.xml

</>复制代码

  1. com.yh
  2. yh_common
  3. 1.0-SNAPSHOT
  4. org.springframework
  5. spring-context
  6. org.springframework
  7. spring-beans
  8. org.springframework
  9. spring-webmvc
  10. org.springframework
  11. spring-jdbc
  12. org.springframework
  13. spring-aspects
  14. org.springframework
  15. spring-jms
  16. org.springframework
  17. spring-context-support
  18. org.springframework
  19. spring-test
  20. org.springframework.security
  21. spring-security-web
  22. org.springframework.security
  23. spring-security-config
  24. com.alibaba
  25. dubbo
  26. org.apache.zookeeper
  27. zookeeper
  28. com.github.sgroschupf
  29. zkclient
  30. junit
  31. junit
  32. com.alibaba
  33. fastjson
  34. org.javassist
  35. javassist
  36. 3.23.1-GA
  37. commons-codec
  38. commons-codec
  39. javax.servlet
  40. servlet-api
  41. provided
  42. com.yh
  43. yh_sellergoods_interface
  44. 1.0-SNAPSHOT
  45. org.csource.fastdfs
  46. fastdfs
  47. commons-fileupload
  48. commons-fileupload
  49. org.apache.tomcat.maven
  50. tomcat7-maven-plugin
  51. 2.2
  52. 9102
  53. /

web.xml

</>复制代码

  1. CharacterEncodingFilter
  2. org.springframework.web.filter.CharacterEncodingFilter
  3. encoding
  4. utf-8
  5. forceEncoding
  6. true
  7. CharacterEncodingFilter
  8. /*
  9. springmvc
  10. org.springframework.web.servlet.DispatcherServlet
  11. contextConfigLocation
  12. classpath:spring/spring*.xml
  13. 2
  14. springmvc
  15. *.do
  16. contextConfigLocation
  17. classpath:spring/spring*.xml,classpath*:spring/applicationContext*.xml
  18. org.springframework.web.context.ContextLoaderListener
  19. springSecurityFilterChain
  20. org.springframework.web.filter.DelegatingFilterProxy
  21. springSecurityFilterChain
  22. /*

pringmvc.xml

</>复制代码

  1. WriteMapNullValue
  2. WriteDateUseDateFormat

Spring-secuity.xml

</>复制代码

com.yh.service.UserDetailServiceImpl.java

</>复制代码

  1. package com.yh.page.service;
  2. import com.alibaba.dubbo.config.annotation.Reference;
  3. import com.yh.pojo.TbSeller;
  4. import com.yh.sellergoods.service.SellerService;
  5. import org.springframework.security.core.GrantedAuthority;
  6. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  7. import org.springframework.security.core.userdetails.User;
  8. import org.springframework.security.core.userdetails.UserDetails;
  9. import org.springframework.security.core.userdetails.UserDetailsService;
  10. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  11. import org.springframework.stereotype.Component;
  12. import java.util.ArrayList;
  13. /
  14. 实现userdetail接口 用于验证前台输入用户信息匹配
  15. 需要让接口扫描到这个类
  16. /
  17. @Component
  18. public class UserDetailServiceImpl implements UserDetailsService {
  19. //远程调用dubbo提供的服务 但是此时还没有
  20. @Reference
  21. public SellerService sellerService;
  22. @Override
  23. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  24. TbSeller seller = sellerService.findOne(username);
  25. System.out.println(seller);
  26. //没找到用户 或者用户没有通过审核
  27. if (seller == null || !"1".equals(seller.getStatus())) {
  28. return null;
  29. }
  30. ArrayList list = new ArrayList<>();
  31. list.add(new SimpleGrantedAuthority("ROLE_SELLER"));
  32. return new User(username, seller.getPassword(), list);
  33. }
  34. }

config/fdfs_client.conf

</>复制代码

  1. # connect timeout in seconds
  2. # default value is 30s
  3. connect_timeout=30
  4. # network timeout in seconds
  5. # default value is 30s
  6. network_timeout=60
  7. # the base path to store log files
  8. base_path=/home/fastdfs
  9. # tracker_server can ocur more than once, and tracker_server format is
  10. # "host:port", host can be hostname or ip address
  11. tracker_server=172.16.224.128:22122
  12. #standard log level as syslog, case insensitive, value list:
  13. ### emerg for emergency
  14. ### alert
  15. ### crit for critical
  16. ### error
  17. ### warn for warning
  18. ### notice
  19. ### info
  20. ### debug
  21. log_level=info
  22. # if use connection pool
  23. # default value is false
  24. # since V4.05
  25. use_connection_pool = false
  26. # connections whose the idle time exceeds this time will be closed
  27. # unit: second
  28. # default value is 3600
  29. # since V4.05
  30. connection_pool_max_idle_time = 3600
  31. # if load FastDFS parameters from tracker server
  32. # since V4.05
  33. # default value is false
  34. load_fdfs_parameters_from_tracker=false
  35. # if use storage ID instead of IP address
  36. # same as tracker.conf
  37. # valid only when load_fdfs_parameters_from_tracker is false
  38. # default value is false
  39. # since V4.05
  40. use_storage_id = false
  41. # specify storage ids filename, can use relative or absolute path
  42. # same as tracker.conf
  43. # valid only when load_fdfs_parameters_from_tracker is false
  44. # since V4.05
  45. storage_ids_filename = storage_ids.conf
  46. #HTTP settings
  47. http.tracker_server_port=80
  48. #use "#include" directive to include HTTP other settiongs
  49. ##include http.conf

com.yh.shop.controller.UploadController

</>复制代码

  1. package com.yh.shop.controller;
  2. import entity.Result;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import org.springframework.web.multipart.MultipartFile;
  7. import utils.FastDFSClient;
  8. // 文件上传controller
  9. @RestController
  10. public class UploadController {
  11. @Value("${FILE_SERVER_URL}")
  12. public String FILE_SERVER_URL;
  13. @RequestMapping("upload")
  14. public Result upload(MultipartFile file) {
  15. //文件名称
  16. //String originalFilename = file.getOriginalFilename();
  17. //获取扩展名称
  18. //String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
  19. String extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
  20. try {
  21. //创建fastdfs客户端
  22. FastDFSClient fastDFSClient = new FastDFSClient("classpath:config/fdfs_client.conf");
  23. //返回图片路径
  24. String path = fastDFSClient.uploadFile(file.getBytes(), extName);
  25. System.out.println(path);
  26. return new Result(true, FILE_SERVER_URL + path);
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. return new Result(false, "上传失败");
  30. }
  31. }
  32. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77085.html

相关文章

  • 把「策略模式」应用到实际项目中

    摘要:阅读原文把策略模式应用到实际项目中无论你知不知道这个设计模式,但必定在项目中都似曾相识。文件存储的方式不同,同时文件的获取和删除也不同保存,获取,删除后的响应也是相同的,也不考虑了。此时引入是解决问题的最佳方式。 阅读原文:把「策略模式」应用到实际项目中 无论你知不知道这个设计模式,但必定在项目中都似曾相识。倘若仅仅聊理论必然枯燥乏味,只有理论和实战相结合方可达到人剑合一的境界。 首先...

    LeviDing 评论0 收藏0
  • Spring Security

    摘要:框架具有轻便,开源的优点,所以本译见构建用户管理微服务五使用令牌和来实现身份验证往期译见系列文章在账号分享中持续连载,敬请查看在往期译见系列的文章中,我们已经建立了业务逻辑数据访问层和前端控制器但是忽略了对身份进行验证。 重拾后端之Spring Boot(四):使用JWT和Spring Security保护REST API 重拾后端之Spring Boot(一):REST API的搭建...

    keelii 评论0 收藏0
  • SpringSecurity系列01】初识SpringSecurity

    摘要:什么是是一个能够为基于的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它来自于,那么它与整合开发有着天然的优势,目前与对应的开源框架还有。通常大家在做一个后台管理的系统的时候,应该采用判断用户是否登录。 ​ 什么是SpringSecurity ? ​ Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全...

    elva 评论0 收藏0
  • 农民进城之初尝FastDFS搭建图片分布式系统

    摘要:新建文件夹尝试启动为默认监听端口看到已经在监听端口,说明启动成功。修改修改为的路径,我这里为修改为你的监听的和端口号,我这里为保存退出。即为上传图片成功 于前不久,公司论坛的图片终于将服务器给挤爆了,已经达到了恐怖的34G,服务器总容量才40G。如果直接加硬盘的话,那么discuz中的逻辑几乎就要全改,所以不行。如果将所有图片扔到对象存储的话,那么这会是一大笔支出(虽然钱不是我出),所...

    LiveVideoStack 评论0 收藏0
  • 农民进城之初尝FastDFS搭建图片分布式系统

    摘要:新建文件夹尝试启动为默认监听端口看到已经在监听端口,说明启动成功。修改修改为的路径,我这里为修改为你的监听的和端口号,我这里为保存退出。即为上传图片成功 于前不久,公司论坛的图片终于将服务器给挤爆了,已经达到了恐怖的34G,服务器总容量才40G。如果直接加硬盘的话,那么discuz中的逻辑几乎就要全改,所以不行。如果将所有图片扔到对象存储的话,那么这会是一大笔支出(虽然钱不是我出),所...

    Cheriselalala 评论0 收藏0

发表评论

0条评论

K_B_Z

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<