博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Security
阅读量:3914 次
发布时间:2019-05-23

本文共 5713 字,大约阅读时间需要 19 分钟。

Spring Security

遇到的问题与解决方案

  1. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘myUserDetailsServiceImpl’: Unsatisfied dependency expressed through field ‘accountDao’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘accountDao’ defined in file [D:\BackDevelop\IDEA\IdeaProjects\springsecurity2\target\classes\com\ahdeer\dao\AccountDao.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required
    翻译如下:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为’myUserDetailsS​​erviceImpl’的bean时出错:通过字段’accountDao’表示的不满足的依赖关系;
    嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建文件[D:\ BackDevelop \ IDEA \ IdeaProjects \ springsecurity2 \ target \ classes \ com \ ahdeer \ dao \ AccountDao.class]中定义的名称为’accountDao’的bean时出错
    :调用init方法失败;嵌套的异常为java.lang.IllegalArgumentException:必需属性’sqlSessionFactory’或’sqlSessionTemplate’
    解决
    CSDN上说的一样的太多了,对我来说都没啥用,我是自己在遇到一个问题时,将
    在这里插入图片描述
    改为:
    在这里插入图片描述
    然后:
    在这里插入图片描述
    所以我自己得出结论是:
    @MapperScan
    @SpringBootApplication
    这两个可以同时用,但是当变为:
    @MapperScan
    @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})时,就会报错,关键你还找不出来,我查为什么会这样,也差不多,只能以后尽量在dao接口上写@Mapper,不写@MapperScan,至于问我,如果主方法类只写
    @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})会如何,我只能说还是报错,如下图:
    在这里插入图片描述
    看不见的单词是found。暂时不懂为什么。
  2. 对上面的一个补充:关于我为什么要用exclude= {DataSourceAutoConfiguration.class}的补充。
    问题描述
    Description:Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.
    Reason: Failed to determine a suitable driver class
    Action:
    Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
    然后我找,都说加上这个exclude= {DataSourceAutoConfiguration.class},但是加上这个和@MapperScan就会报第一个错误,但是别人就不会,而且我要是只留下@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
    则会报上面的最后一张图的那个问题。不知道怎么回事,学的不深。
  3. 直接上报错问题
    java.sql.SQLException: Access denied for user ‘’@‘localhost’ (using password: YES)
    2021-03-06 16:59:35.022 ERROR 8236 — [nio-8777-exec-4] w.a.UsernamePasswordAuthenticationFilter : An internal error occurred while trying to authenticate the user.
    org.springframework.security.authentication.InternalAuthenticationServiceException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
    Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user ‘’@‘localhost’ (using password: YES)
    The error may exist in com/ahdeer/dao/AccountDao.java (best guess)
    The error may involve com.ahdeer.dao.AccountDao.queryByAccName
    The error occurred while executing a query
    Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user ‘’@‘localhost’ (using password: YES)
    Caused by: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
    Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user ‘’@‘localhost’ (using password: YES)
    The error may exist in com/ahdeer/dao/AccountDao.java (best guess)
    The error may involve com.ahdeer.dao.AccountDao.queryByAccName
    The error occurred while executing a query
    Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user ‘’@‘localhost’ (using password: YES)
    Caused by: org.apache.ibatis.exceptions.PersistenceException: Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user ‘’@‘localhost’ (using password: YES)
    The error may exist in com/ahdeer/dao/AccountDao.java (best guess)
    The error may involve com.ahdeer.dao.AccountDao.queryByAccName
    The error occurred while executing a query
    Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user ‘’@‘localhost’ (using password: YES)
    Caused by: java.sql.SQLException: Access denied for user ‘’@‘localhost’ (using password: YES)
    解决
    在这里插入图片描述看出来了吗?spring.datasource.username=root 才对。还是手敲靠谱,以后眼睛放亮点。
  4. localhost 将您重定向的次数过多
    在这里插入图片描述
    解决在这里插入图片描述这肯定是被拦截了,因为我少加了这个。
  5. 无效的绑定语句
    在这里插入图片描述
    解决:报这个错不要怕,哈哈哈,其实我老是忘了写sql语句,因为我用的是mybatis,学习阶段,老是丢三落四(在方法的上面加上 @Select ( select * from account) )。
  6. 连不上数据库,因为未输入密码(有的时候输入了也连不上)
    在这里插入图片描述
    解决:我不说,很难看到错误,因为平时我是自己敲,不看提示,但是这次偷懒,用了提示,把spring.datasource.password 多写了一个 data 。一般连不上数据库,可以从这里面找。

Security注解大全

  1. @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
    开启基于方法的安全认证机制,也就是说在web层的config/main方法 启用注解机制的安全确认(说人话就是用了这个注解才能用以下的注解,这个注解可以写在config或者main方法上面)
  2. @Secured:
    在这里插入图片描述
  3. @PerAuthorize:
    在这里插入图片描述
  4. @PostAuthorize:
    在这里插入图片描述关于这个注解有什么用,我截图举个例子:
    在这里插入图片描述
    这是只定义了我可以访问的权限后,我查询成功,并且把数据给我看了,并且println打印成功。但是当我用没有权限的孟令新访问,则是跳转到403页面,且controller那个方法并没有执行。
    在这里插入图片描述
    这是我加了这个注解后执行的结果,但是用这个注解前,不需要再写谁谁谁有没有权限了,所以把之前定义权限的语句注释掉,但是在用了这个后,我还是能访问admin才能看到的表中信息,但是孟令新却依然跳转到403页面。但是,在我的控制台打印出了那个方法,因为这个注释的执行是在方法之后,就是说,方法先执行,再看有没有权限。
    那肯定好奇,这有啥用?
    我来说个例子:
    QQ空间有设置访问权限的功能对吧,有的人能访问,有的不行,但是不管行不行,账号主人总是能看到访问记录(就像我控制台打印的记录),用处可以看到我屏蔽过的人的是不是还想看我空间,可以则甄别他是哪种人,难道想骚扰我?!天天访问?!哈哈哈,开玩笑,不过挺有意思的。
  5. @PreFilter 和 @PostFilter(前者是通过在括号内写条件,符合条件的可以执行方法;后者是符合条件的执行,但是是先执行方法,再判断条件)这里只放后者截图:
    在这里插入图片描述

转载地址:http://qzprn.baihongyu.com/

你可能感兴趣的文章
骚年快答 | 微服务架构中的BFF到底是啥?
查看>>
设计模式之适配器模式
查看>>
如何利用Gitlab-CI持续部署到远程机器?
查看>>
.NET Core + K8S + Loki 玩转日志聚合
查看>>
ASP.NET Core中的分布式缓存
查看>>
在ASP.NET Core中创建自定义端点可视化图
查看>>
继续分享 5 个实用的 vs 调试技巧
查看>>
五年了,别再把务虚会开 “虚” 了
查看>>
一文看懂"async"和“await”关键词是如何简化了C#中多线程的开发过程
查看>>
每天都在支付,你真的了解信息流和资金流?
查看>>
.Net Core 自定义配置源从配置中心读取配置
查看>>
基于.NetCore3.1系列 —— 日志记录之日志配置揭秘
查看>>
设计模式之享元模式
查看>>
单例模式最佳实践
查看>>
.NET Core + Spring Cloud:服务注册与发现
查看>>
今天你内卷了吗?
查看>>
设计模式之代理模式
查看>>
在 MySQL 中使用码农很忙 IP 地址数据库
查看>>
结构型设计模式总结
查看>>
dotNET:怎样处理程序中的异常(实战篇)?
查看>>