- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我将 spring Boot MVC Security 与 JPA Hibernate 和 Thymeleaf 模板引擎一起使用。我正在尝试使用一些已存储在数据库 (MySQL) 中的用户创建用户登录。每当我使用正确的凭据时,都可以。此外,当我使用正确的用户名和错误的密码时,它会正常工作,因为它应该显示一条错误消息。但是,当我提供一个不在表(用户)上的不存在的用户名或将该字段留空时,我有一个错误:
ERROR 11759 --- [nio-8080-exec-5] w.a.UsernamePasswordAuthenticationFilter : An internal error occurred while trying to authenticate the user."
以下是我可能有用的代码:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailServiceConfig myAppUserDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/home/**").hasAnyRole("ADMIN","USER")
.and().formLogin() //login configuration
.loginPage("/login")
.loginProcessingUrl("/app-login")
.usernameParameter("app_username")
.passwordParameter("app_password")
.defaultSuccessUrl("/home")
.and().logout() //logout configuration
.logoutUrl("/app-logout")
.logoutSuccessUrl("/login")
.and().exceptionHandling() //exception handling configuration
.accessDeniedPage("/error");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
auth.userDetailsService(myAppUserDetailsService).passwordEncoder(passwordEncoder);
}
}
Controller :
@Controller
@RequestMapping("/")
public class HomeController {
@Autowired
private UserInfoService userInfoService;
@GetMapping("login")
public ModelAndView login() {
ModelAndView mav = new ModelAndView();
mav.setViewName("login");
return mav;
}
@GetMapping("home")
public ModelAndView getAllUserArticles() {
ModelAndView mav = new ModelAndView();
mav.addObject("userArticles", userInfoService.getAllUserArticles());
mav.setViewName("articles");
return mav;
}
@GetMapping("error")
public ModelAndView error() {
ModelAndView mav = new ModelAndView();
mav.setViewName("error");
return mav;
}
}
用户详情服务
public class UserDetailServiceConfig implements UserDetailsService {
@Autowired
private UserInfoDao userInfoDAO;
@Override
public UserDetails loadUserByUsername(String userName)
throws UsernameNotFoundException {
UserInfo activeUserInfo = userInfoDAO.getActiveUser(userName);
GrantedAuthority authority = new SimpleGrantedAuthority(activeUserInfo.getRole());
UserDetails userDetails = (UserDetails)new User(activeUserInfo.getUserName(),
activeUserInfo.getPassword(), Arrays.asList(authority));
return userDetails;
}
}
错误:
ERROR 12337 --- [nio-8080-exec-5] w.a.UsernamePasswordAuthenticationFilter : An internal error occurred while trying to authenticate the user.
org.springframework.security.authentication.InternalAuthenticationServiceException: A granted authority textual representation is required
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:126) ~[spring-security-core-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:144) ~[spring-security-core-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:174) ~[spring-security-core-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199) ~[spring-security-core-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:124) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:347) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:263) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.23.jar:8.5.23]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_131]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_131]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.23.jar:8.5.23]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]
Caused by: java.lang.IllegalArgumentException: A granted authority textual representation is required
at org.springframework.util.Assert.hasText(Assert.java:181) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.security.core.authority.SimpleGrantedAuthority.<init>(SimpleGrantedAuthority.java:38) ~[spring-security-core-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at com.example.sample.configuration.UserDetailServiceConfig.loadUserByUsername(UserDetailServiceConfig.java:25) ~[classes/:na]
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:114) ~[spring-security-core-4.2.3.RELEASE.jar:4.2.3.RELEASE]
... 45 common frames omitted
最佳答案
@PaulNUK 是的,我必须更改我的 UserDetailsService 代码以检查数据库中是否存在任何不存在的用户并且它有效。我已经更正了。谢谢大家!
@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
UserInfo activeUserInfo = userInfoDAO.getActiveUser(userName);
String dBuserName = activeUserInfo.getUserName();
if(dBuserName == null){
throw new UsernameNotFoundException("User not authorized.");
}
GrantedAuthority authority = new SimpleGrantedAuthority(activeUserInfo.getRole());
UserDetails userDetails = (UserDetails)new User(dBuserName,
activeUserInfo.getPassword(), Arrays.asList(authority));
return userDetails;
}
关于java - w.a.UsernamePasswordAuthenticationFilter : An internal error occurred while trying to authenticate the user (ERROR 11759),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48111240/
谁能用一个例子来解释 BooleanQuery 中 lucene 中的 BooleanClause.Occur.Must 和 BooleanClause.Occur.SHOULD 之间的区别? 最佳答
正如标题所说,multiset 在所有相同值的范围末尾插入一个值。 (例如:在多重集 1,2,2,3 中插入 2 使其成为 1,2,2,/*new*/2,3)。 如何在所有相同值范围的开头插入新值?
所以这是与此(Inserting in a multiset: before the first occurence of that value instead of after the last o
我试图从我的 WCF .Net Framework 4.5 向 API rest 发布一个文件。这是我的代码: public string CreateConclusion(string[] inst
我的 SQL 查询获取固件的错误修复验证列表,例如def-456 是一张票,要求我对产品进行固件测试。 def-456 有几个记录结果的子任务。结果记录为:id:abc-123、abc-124、abc
我想删除文件中多次出现的行,但想保留某些行。我该怎么做? 这是我的文件的一部分,我想更改它: §M: 1, K: 2 name, time, cycle, instr, L1-mi
我正在 SSMS 中测试 SQL 2016 Live Query Stats,每次尝试时都会收到错误消息“执行批处理时出错。错误消息是:发生一个或多个错误。”并且不返回任何结果集。一位同事试过了,对他
我们在 JBoss 4.2 上设置了一个水平集群。在我们将缓存模式从 REPL_ASYNC 更改为 REPL_SYNC 以解决问题之前, session 复制工作正常。我们开始看到一些 session
我正在尝试将 MVC 网站发布为 Azure 网络角色。 当我在本地运行它时,一切正常。 但是当我将其发布到 Azure 并浏览某些 MVC 操作时,我收到此错误: Server Error in '
假设一个静态库 libfoo 依赖于另一个静态库 libbar 的某些功能。这些和我的应用程序都是用 D 编写的。如果我的应用程序只直接使用 libfoo,并且只调用 libfoo 中的函数而不引用
我正在尝试在 Eclipse Helios 上安装 SVN 客户端, 我已经从 Collaboration 节点安装了所有 SVN 模块(更新中), 现在重启后我可以选择一个连接器 出现“颠覆性连接器
我在 cakephp 中有一些代码会产生错误。 这是 PHP Controller : $this->loadModel( 'Vote' ); //Newly added by amit start
我需要有关 Java 代码的帮助。 这就是问题所在: 输入示例:AaaaaAa 输出:A 出现 7。 问题是我需要它来忽略案例。 请帮助我,我的代码工作正常,只是它不忽略大小写。 import jav
我正在为 J2ME 开发一个应用程序,有时它完全卡住并且 AMS 需要相当长的时间来关闭它.在我看来,这像是一个死锁问题。 你能告诉我什么会导致死锁吗?例如,如果对象调用其自身的另一个同步方法,调用对
尝试将 DEXguard 安装到 Eclipse 中的简单应用程序时出现以下错误: Errors occurred during the build. Errors running builder '
在 SAS 中,假设我有一个名为“person_groups”的数据集。它有两个变量,名为“person”和“group”。该数据集只是将每个人分配到一个组。 我如何从这个数据集中删除所有在他们的组中
有人知道如何在表达式中找到第 n 次出现的字符串以及如何用正则表达式替换它吗? 例如我有以下字符串 txt sub("(^(.*?-){4}.*?)-(.*?-.*?)-", "\\1|\\3||"
是否有一个包允许我为同一个缓冲区设置多个 Occur 结果缓冲区(例如 grep-a-lot: http://www.emacswiki.org/emacs/grep-a-lot.el )。 我在分析
我一直在寻找这个,但似乎无法找到它。 我有一个带有 try {} catch {} 语句的脚本。如果没有发生错误,我想添加一个操作。 例如 try { something } catch { "Err
我正在从 iPhone 应用程序将照片上传到 Facebook。我已经让它工作了,只是有时它会返回“发生未知错误”。我不确定问题是什么。这种情况发生的概率约为 75%。 其他人也遇到过这种情况吗? 最
我是一名优秀的程序员,十分优秀!