本文共 5713 字,大约阅读时间需要 19 分钟。
Spring Security
遇到的问题与解决方案:
- 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:创建名称为’myUserDetailsServiceImpl’的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。暂时不懂为什么。
- 对上面的一个补充:关于我为什么要用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}) 则会报上面的最后一张图的那个问题。不知道怎么回事,学的不深。
- 直接上报错问题: 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 才对。还是手敲靠谱,以后眼睛放亮点。
- localhost 将您重定向的次数过多: 解决:这肯定是被拦截了,因为我少加了这个。
- 无效的绑定语句: 解决:报这个错不要怕,哈哈哈,其实我老是忘了写sql语句,因为我用的是mybatis,学习阶段,老是丢三落四(在方法的上面加上 @Select ( select * from account) )。
- 连不上数据库,因为未输入密码(有的时候输入了也连不上): 解决:我不说,很难看到错误,因为平时我是自己敲,不看提示,但是这次偷懒,用了提示,把spring.datasource.password 多写了一个 data 。一般连不上数据库,可以从这里面找。
Security注解大全:
- @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 开启基于方法的安全认证机制,也就是说在web层的config/main方法 启用注解机制的安全确认(说人话就是用了这个注解才能用以下的注解,这个注解可以写在config或者main方法上面)
- @Secured:
- @PerAuthorize:
- @PostAuthorize: 关于这个注解有什么用,我截图举个例子: 这是只定义了我可以访问的权限后,我查询成功,并且把数据给我看了,并且println打印成功。但是当我用没有权限的孟令新访问,则是跳转到403页面,且controller那个方法并没有执行。 这是我加了这个注解后执行的结果,但是用这个注解前,不需要再写谁谁谁有没有权限了,所以把之前定义权限的语句注释掉,但是在用了这个后,我还是能访问admin才能看到的表中信息,但是孟令新却依然跳转到403页面。但是,在我的控制台打印出了那个方法,因为这个注释的执行是在方法之后,就是说,方法先执行,再看有没有权限。 那肯定好奇,这有啥用? 我来说个例子: QQ空间有设置访问权限的功能对吧,有的人能访问,有的不行,但是不管行不行,账号主人总是能看到访问记录(就像我控制台打印的记录),用处可以看到我屏蔽过的人的是不是还想看我空间,可以则甄别他是哪种人,难道想骚扰我?!天天访问?!哈哈哈,开玩笑,不过挺有意思的。
- @PreFilter 和 @PostFilter(前者是通过在括号内写条件,符合条件的可以执行方法;后者是符合条件的执行,但是是先执行方法,再判断条件)这里只放后者截图:
转载地址:http://qzprn.baihongyu.com/