- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试配置 Spring 安全身份验证类,以便对访问应用程序的用户进行身份验证。在集成数据库之前,我使用纯文本文件作为占位符。问题是我在应用程序启动时收到错误。
我的类(class)是
@Configuration
@EnableWebSecurity
public class CustomWebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Autowired
private ServletContext servletContext;
private static final Logger logger = LoggerFactory.getLogger(CustomWebSecurityConfigurer.class);
private static ArrayList<String> allowedUrlsForUsers = new ArrayList<String>();
private static ArrayList<String> allowedUrlsForAdmins = new ArrayList<String>();
static {
BufferedReader reader = null;
try {
ClassLoader classLoader = Application.class.getClassLoader();
File file = new File(classLoader.getResource("allowed-pages").getFile());
reader = new BufferedReader(
new InputStreamReader(new FileInputStream(file), ProjectConstants.getProjectEncoding()));
String currentLine = null;
while ((currentLine = reader.readLine()) != null) {
if (!currentLine.isEmpty()) {
String[] allowedPage = currentLine.split(Pattern.quote(ProjectConstants.getDelimiter()));
if (allowedPage.length == 2) {
String[] roles = allowedPage[1].split(Pattern.quote(ProjectConstants.getSecondaryDelimiter()));
for (String role : roles) {
if (role.equals(ProjectConstants.getRoleUser()))
allowedUrlsForUsers.add(allowedPage[0]);
if (role.equals(ProjectConstants.getRoleAdmin()))
allowedUrlsForAdmins.add(allowedPage[0]);
}
}
}
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/css/**", "/js/**").permitAll().and().authorizeRequests()
.antMatchers(allowedUrlsForAdmins.toArray(new String[0])).hasRole("ADMIN")
.antMatchers(allowedUrlsForUsers.toArray(new String[0])).hasRole("USER").anyRequest().authenticated()
.and().formLogin().loginPage("/login").defaultSuccessUrl(servletContext.getContextPath() + "/dashboard")
.permitAll().and().logout().logoutUrl(servletContext.getContextPath() + "/logout")
.logoutSuccessUrl(servletContext.getContextPath() + "/login?logout").clearAuthentication(true)
.invalidateHttpSession(true).deleteCookies("JSESSIONID").permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(new CustomAuthenticationProvider());
}
}
和
public class CustomAuthenticationProvider implements AuthenticationProvider {
private static final Logger logger = LoggerFactory.getLogger(CustomAuthenticationProvider.class);
private static HashMap<String, User> credentialRepository = new HashMap<String, User>();
static {
BufferedReader reader = null;
try {
ClassLoader classLoader = Application.class.getClassLoader();
File file = new File(classLoader.getResource("credentials").getFile());
reader = new BufferedReader(
new InputStreamReader(new FileInputStream(file), ProjectConstants.getProjectEncoding()));
String currentLine = null;
while ((currentLine = reader.readLine()) != null) {
if (!currentLine.isEmpty()) {
String[] credential = currentLine.split(Pattern.quote(ProjectConstants.getDelimiter()));
if (credential.length == 3) {
String[] roles = credential[2].split(Pattern.quote(ProjectConstants.getSecondaryDelimiter()));
User user = new User(credential[0], credential[1], roles);
credentialRepository.put(user.username, user);
}
}
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
User user = credentialRepository.get(authentication.getName().toLowerCase());
if (user != null) {
if (user.password.equals(authentication.getCredentials())) {
List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
for (String role : user.roles) {
auths.add(new SimpleGrantedAuthority(role));
}
return new UsernamePasswordAuthenticationToken(user.username, user.password, auths);
} else {
throw new BadCredentialsException("Invalid Password");
}
} else {
throw new UsernameNotFoundException("Unknown Username");
}
}
@Override
public boolean supports(Class<?> arg0) {
return true;
}
private static class User {
String username;
String password;
ArrayList<String> roles;
public User(String username, String password, String[] roles) {
this.username = username;
this.password = password;
this.roles = new ArrayList<String>();
for (String role : roles) {
this.roles.add(role);
}
}
}
}
当我运行应用程序时,会打印堆栈跟踪并且启动失败。
[ERROR] 2017-10-09 22:11:14.494 SpringApplication - Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'inMemoryUserDetailsManager' defined in class path resource [org/springframework/boot/autoconfigure/security/AuthenticationManagerConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.provisioning.InMemoryUserDetailsManager]: Factory method 'inMemoryUserDetailsManager' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.security.provisioning.InMemoryUserDetailsManager.<init>([Lorg/springframework/security/core/userdetails/UserDetails;)V
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:605) ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1249) ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1098) ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:756) ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.0.0.RC4.jar:5.0.0.RC4]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.0.0.RC4.jar:5.0.0.RC4]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:122) ~[spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at me.brkn.raspberrydashboard.Application.main(Application.java:15) [classes/:?]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.provisioning.InMemoryUserDetailsManager]: Factory method 'inMemoryUserDetailsManager' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.security.provisioning.InMemoryUserDetailsManager.<init>([Lorg/springframework/security/core/userdetails/UserDetails;)V
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:186) ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:597) ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]
... 18 more
Caused by: java.lang.NoSuchMethodError: org.springframework.security.provisioning.InMemoryUserDetailsManager.<init>([Lorg/springframework/security/core/userdetails/UserDetails;)V
at org.springframework.boot.autoconfigure.security.AuthenticationManagerConfiguration.inMemoryUserDetailsManager(AuthenticationManagerConfiguration.java:61) ~[spring-boot-autoconfigure-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.boot.autoconfigure.security.AuthenticationManagerConfiguration$$EnhancerBySpringCGLIB$$9c5d1909.CGLIB$inMemoryUserDetailsManager$0(<generated>) ~[spring-boot-autoconfigure-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.boot.autoconfigure.security.AuthenticationManagerConfiguration$$EnhancerBySpringCGLIB$$9c5d1909$$FastClassBySpringCGLIB$$54415980.invoke(<generated>) ~[spring-boot-autoconfigure-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-5.0.0.RC4.jar:5.0.0.RC4]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361) ~[spring-context-5.0.0.RC4.jar:5.0.0.RC4]
at org.springframework.boot.autoconfigure.security.AuthenticationManagerConfiguration$$EnhancerBySpringCGLIB$$9c5d1909.inMemoryUserDetailsManager(<generated>) ~[spring-boot-autoconfigure-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_131]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_131]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:155) ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:597) ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]
... 18 more
我正在使用 Spring Boot Web 和安全启动器。我很乐意提供任何建议。
编辑1:我的依赖声明如下。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<properties>
<java.version>1.8</java.version>
<tomcat.version>8.5.14</tomcat.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
提前致谢。
最佳答案
您必须包含此内容。
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
</dependency>
关于java - 创建名称为 'inMemoryUserDetailsManager' 的 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46653844/
有谁知道如何实现称为 PRUNING 的图像处理技术?任何想法,例子等?我正在使用 OpenCV 和 C#,如果有人可以提供帮助,我将不胜感激。 最佳答案 我假设您希望从图像中移除不需要的杂散和伪影。
假设我已经构建了一个可调用对象数组 callables = [] callables << block1 callables << block2 callables << block3 callabl
这个问题在这里已经有了答案: std::shared_ptr of this (2 个答案) 关闭 8 年前。 我正在学习 C++11 特性,特别是 shared_ptr,我在引用 this 并将其
我主要是一个oracle新手,所以如果这是一个愚蠢的问题,请原谅我...... 我已经阅读了很多关于 authID 的文章,我的理解是 AUTHID 子句指示 Oracle 是否以调用者的权限 (CU
我使用的是 Python 2.7。 按照惯例,__iter__、__init__、__str__ 等内置方法使用双下划线来标识它们。为什么 next() 函数没有这个? 它与这里的其他内置函数一起
这是疏忽吗?还是跟JVM有关? 最佳答案 Java 确实有指针——不能在指针上执行指针运算。 来自可敬的JLS : There are two kinds of types in the Java p
我越来越多地发现宣传“原生”桌面应用程序的服务,但在下载或研究后发现它们是在 Electron 或类似的 Web View 框架中构建的 Web View 。他们误解了“本地人”还是我? 另外,我可以
我尝试在 Wordpress 中使用,但它在控制台中显示 jstree 不是函数在 header PHP 文件中: /dist/libs/jquery.js"> /dist/themes/defaul
我想让 scons 调用带有目录列表的 nosetests。执行此操作的最佳方法是什么? 最佳答案 如果您需要分析外部应用程序的返回码(例如,如果您调用测试),您需要使用 Command() + py
这是一些尝试使用 OOB(紧急)数据的基本代码。我的问题是,如果客户端使用 C 或 Java,则服务器部分的行为不同。请注意,您可能认为这两个客户端都有些棘手,但如果我使用 C 服务器(以更好地控制
虚拟文件系统 (VFS) 或虚拟文件系统交换机是更具体文件系统之上的抽象层。 VFS的目的是允许客户端应用程序以统一的方式访问不同类型的具体文件系统。 如果我们看到 VFS 的实际工作,这个定义似乎是
"Effective Go"状态: By convention, one-method interfaces are named by the method name plus an -er suff
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭 3 个月前。 社区审核了是否
在深入研究 Scala 时,我遇到了 这个词。类型类 . 它一直令人困惑,因为一个类是一个类型,而一个类型可以 在 Scala 中是一个类,“类型”和“类”本身就是抽象术语。 在阅读了一些示例后,我感
我正在尝试编写一个简单地杀死我的程序的函数。它被称为退出聊天。但是,当我调用函数 messenger:quitChat() 时,出现此错误。 ** exception error: bad argum
我正在尝试编写一个简单地杀死我的程序的函数。它被称为退出聊天。但是,当我调用函数 messenger:quitChat() 时,出现此错误。 ** exception error: bad argum
如果您向 DB2/zOS 引擎 DBA 询问有关 DB2 行为的问题,DBA 会将 DB2 引擎称为“他”,就像水手使用“她”来指代他的船一样。 例如:“一旦您填充了可用空间,DB2 仍希望在表空间中
案例:我有 2 个具有以下 OneToMany 关系的表:TagAbstract 1..* TagConf,其中: tag_abstract_ID 是一个字段,用于标识许多 TagConf 记录,这些
我有一个表格,我想让第一行“自动固定”(这意味着当向下滚动时它固定在顶部,而如果不向下滚动,它就不固定)。这是代码:``` table { background: yellow; } td
其他动词对我来说都有意义,但我对“发布”作为动词没有太多上下文。它是像在邮局发帖(这有一定道理,虽然看起来有点牵强)还是像在公告板上发帖(对我来说意义不大)?有谁知道是谁决定了“POST”以及为什么选
我是一名优秀的程序员,十分优秀!