资讯专栏INFORMATION COLUMN

SpringSecurity01(使用传统的xml方式开发,且不连接数据库)

Gilbertat / 2024人阅读

摘要:创建一个工程在里面添加依赖,依赖不要随便改我改了出错了好几次都找不到原因可以轻松的将对象转换成对象和文档同样也可以将转换成对象和配置

1.创建一个web工程
2.在pom里面添加依赖,依赖不要随便改,我改了出错了好几次都找不到原因

</>复制代码

  1. UTF-8
  2. 1.7
  3. 1.7
  4. 2.5.0
  5. 1.2
  6. 3.0-alpha-1
  7. org.springframework
  8. spring-webmvc
  9. 4.3.13.RELEASE
  10. org.springframework
  11. spring-web
  12. 4.3.13.RELEASE
  13. org.springframework
  14. spring-context
  15. 4.3.13.RELEASE
  16. org.springframework
  17. spring-beans
  18. 4.3.13.RELEASE
  19. org.springframework
  20. spring-core
  21. 4.3.13.RELEASE
  22. org.springframework.security
  23. spring-security-config
  24. 4.2.3.RELEASE
  25. org.springframework.security
  26. spring-security-web
  27. 4.2.3.RELEASE
  28. com.fasterxml.jackson.core
  29. jackson-core
  30. ${jacksonVersion}
  31. com.fasterxml.jackson.core
  32. jackson-annotations
  33. ${jacksonVersion}
  34. com.fasterxml.jackson.core
  35. jackson-databind
  36. ${jacksonVersion}
  37. org.codehaus.jackson
  38. jackson-mapper-asl
  39. 1.9.13
  40. org.codehaus.jackson
  41. jackson-core-asl
  42. 1.9.13
  43. jstl
  44. jstl
  45. ${jstlVersion}
  46. javax.servlet
  47. servlet-api
  48. ${servletVersion}
  49. provided
  50. javax.servlet.jsp
  51. jsp-api
  52. 2.2
  53. provided

3.配置web.xml

</>复制代码

  1. user-manager
  2. index.jsp
  3. contextConfigLocation
  4. classpath:applicationContext.xml
  5. classpath:springSecurity.xml
  6. org.springframework.web.context.ContextLoaderListener
  7. springSecurityFilterChain
  8. org.springframework.web.filter.DelegatingFilterProxy
  9. springSecurityFilterChain
  10. /*
  11. encodingFilter
  12. org.springframework.web.filter.CharacterEncodingFilter
  13. encoding
  14. UTF-8
  15. forceEncoding
  16. true
  17. encodingFilter
  18. /*
  19. spring
  20. org.springframework.web.servlet.DispatcherServlet
  21. contextConfigLocation
  22. classpath:springmvc.xml
  23. 1
  24. spring
  25. /

4.配置applicationContext.xml

</>复制代码

5.配置springmvc.xml

</>复制代码

  1. text/plain;charset=utf-8
  2. text/html;charset=UTF-8

6.配置springSecurity.xml

</>复制代码

7.自定义成功处理的拦截器 MyAuthenticationSuccessHandler

</>复制代码

  1. public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
  2. private final static ObjectMapper objectMapper=new ObjectMapper();
  3. @Override
  4. public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
  5. //这里是可以拿到用户对的登录名的
  6. String username = request.getParameter("username");
  7. System.out.println(username);
  8. response.setContentType("application/json;charset=utf-8");
  9. String successMessage = objectMapper.writeValueAsString(new JsonData(200, "登陆成功"));
  10. response.getWriter().print(successMessage);
  11. }
  12. }

自定义失败处理器的拦截器

</>复制代码

  1. public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler{
  2. private final static ObjectMapper objectMapper=new ObjectMapper();
  3. @Override
  4. public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
  5. //这里是可以拿到用户对的登录名的
  6. String username = request.getParameter("username");
  7. System.out.println(username);
  8. response.setContentType("application/json;charset=utf-8");
  9. String failureMessage = objectMapper.writeValueAsString(new JsonData(500, "登陆失败"));
  10. response.getWriter().print(failureMessage);
  11. }
  12. }

自定义的authentication-provider下面的user-service
定义一个类,这个实现类就是用于封装数据库里面的用户的信息然后返回给springsecurity,它会比较从
表单穿过来的用户名和密码和数据库查出来的用户名和密码进行比对,这里写死,以后再加数据库

</>复制代码

  1. public class MyUserDetailService implements UserDetailsService {
  2. @Override
  3. public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
  4. //UserDetails:封装用户数据的接口,这里应该是数据库查询出来的数据,当返回这个user对象时,springsecurity会把输入的用户名和密码和这个对象里面的用户名和密码进行比对
  5. //成功则认证通过,失败则登陆失败
  6. User user=new User("jojo","123456", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
  7. return user;
  8. }
  9. }

创建这个目录结构

示范

</>复制代码

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. 商品添加页面
  3. 商品添加页面

其它页面一样
index.jsp

</>复制代码

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. 首页
  3. 一下是网站的功能
  4. 商品添加
  5. 商品修改
  6. 商品查询
  7. 商品删除

login.jsp

</>复制代码

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. 登录页面
  3. 用户名:
  4. 密 码:

errorPage.jsp

</>复制代码

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. 自定义错误页面
  3. 权限不足,请正确操作

controller

</>复制代码

  1. @Controller
  2. public class MainController {
  3. /**
  4. *
  5. * @return登录页面
  6. */
  7. @RequestMapping("/userLogin")
  8. public String loginPage()
  9. {
  10. return "login";
  11. }
  12. /**
  13. *
  14. * @return登录页面
  15. */
  16. @RequestMapping("/error")
  17. public String errorPage()
  18. {
  19. return "errorPage";
  20. }
  21. }

</>复制代码

  1. @Controller
  2. @RequestMapping("product")
  3. public class ProductController {
  4. /**
  5. * 商品添加
  6. */
  7. @RequestMapping("index")
  8. public String index()
  9. {
  10. return "index";
  11. }
  12. /**
  13. * 商品添加
  14. */
  15. @RequestMapping("add")
  16. public String add()
  17. {
  18. return "product/productAdd";
  19. }
  20. /**
  21. * 商品修改
  22. */
  23. @RequestMapping("update")
  24. public String update()
  25. {
  26. return "product/productUpdate";
  27. }
  28. /**
  29. * 商品列表
  30. */
  31. @RequestMapping("list")
  32. public String list()
  33. {
  34. return "product/productList";
  35. }
  36. /**
  37. * 商品删除
  38. */
  39. @RequestMapping("delete")
  40. public String delete()
  41. {
  42. return "product/productDelete";
  43. }
  44. }

创建一个返回给前台的包装类,用于返回code和message

</>复制代码

  1. public class JsonData {
  2. private Integer code;
  3. private String message;
  4. public JsonData() {
  5. }
  6. public JsonData(Integer code, String message) {
  7. this.code = code;
  8. this.message = message;
  9. }
  10. public Integer getCode() {
  11. return code;
  12. }
  13. public void setCode(Integer code) {
  14. this.code = code;
  15. }
  16. public String getMessage() {
  17. return message;
  18. }
  19. public void setMessage(String message) {
  20. this.message = message;
  21. }
  22. }

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

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

相关文章

  • SpringSecurity系列01】初识SpringSecurity

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

    elva 评论0 收藏0
  • SpringSecurity01(springSecurity执行流程02)

    摘要:里面配置的过滤器链当用户使用表单请求时进入返回一个的实例一般是从数据库中查询出来的实例然后直接到最后一个如果有错则抛错给前面一个进行抛错如果没有错则放行可以访问对应的资源上面是总的执行流程下面单独说一下的认证流程这个图应该都看得懂和里面的配 showImg(https://segmentfault.com/img/bVbvO0O?w=1258&h=261);web.xml里面配置的过滤...

    Dr_Noooo 评论0 收藏0
  • springSecurity02(mybatis+springmvc+spring) 01

    摘要:建立一个模块继承上一个模块然后添加依赖解决打包时找不到文件建立数据源文件数据库连接相关修改配置数据源和整合,以及事务管理自动扫描扫描时跳过注解的类控制器扫描配置文件这里指向的是 1.建立一个模块继承上一个模块然后添加依赖 junit junit 4.11 test ...

    FrancisSoung 评论0 收藏0
  • Spring4和SpringSecurity4整合(二)连接mybatis和mysql

    摘要:在上一篇基本配置了一些文件中,基本可以在文件中指定用户名和密码来进行实现的验证,这次和一起来配合使用加入的配置文件别名在的中配置数据源查找配置事物然后建立层,和层以及对应这里省略实 在上一篇基本配置了一些文件中,基本可以在文件中指定用户名和密码来进行实现SpringSecurity的验证,这次和mynatis一起来配合使用 加入mybatis的配置文件: mybatis-config....

    NoraXie 评论0 收藏0
  • Spring4和SpringSecurity4整合(一)

    摘要:的官方文档及其简单,他的示例配置就是在文件中把用户名和密码写固定了,然而在实际工作中是不可能的,参考了下网上的教程发现良莠不齐,特此写下记录学习过程首先导入包配置后面直接写这里会提示出错,提示找不 SpringSecurity的官方文档及其简单,他的示例配置就是在xml文件中把用户名和密码写固定了,然而在实际工作中是不可能的,参考了下网上的教程发现良莠不齐,特此写下记录学习过程首先po...

    sorra 评论0 收藏0

发表评论

0条评论

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