- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个基于 this tutorial 的小型网络应用程序。原始版本按预期工作。然后我做了一些更改,它停止工作,也就是说我可以在不登录的情况下访问/hello 页面。我正在学习这个神奇的自动配置世界,我想了解我的代码和原始代码之间的关键区别在哪里。
所以我有初始化程序,因为我不需要 main
东西,我只想要一个网络应用程序:
public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class, SecurityConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
然后我有这个:
//@EnableWebMvc
// I tried with and without this annotation, no difference
// I guess as I extend WebMvcConfigurerAdapter I don't need this
@ComponentScan
@Configuration
@EnableAutoConfiguration
public class WebConfig {
}
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("home");
registry.addViewController("/").setViewName("home");
registry.addViewController("/hello").setViewName("hello");
registry.addViewController("/login").setViewName("login");
}
}
最后是安全部分:
@配置@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/", "/home").permitAll()
.anyRequest().authenticated();
http.formLogin().loginPage("/login").permitAll().and().logout()
.permitAll();
}
@Configuration
protected static class AuthenticationConfiguration extends
GlobalAuthenticationConfigurerAdapter {
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
}
这些类都在同一个包中。如果有人向我解释这不起作用的原因是什么,我会很高兴。
最佳答案
SecurityConfig.class
需要位于根应用程序上下文中,而不是位于 servlet 应用程序上下文中;因此写出以下内容
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { SecurityConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
}
在您的Initializer
类中。
您可能想查看这篇博文:https://spring.io/blog/2013/07/03/spring-security-java-config-preview-web-security .
关于java - 我的 Spring SecurityConfig 没有被获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25411616/
我正在尝试使用 Spring Security,所有这些都使用一个角色。 protected void configure(HttpSecurity http) throws Exception {
我是 Spring 新手,正在尝试使用 Spring 为 RESTful API 创建身份验证。我收到以下错误,如何解决此问题? 堆栈跟踪: Error creating bean with name
我正在尝试使用 Spring Security,所有这些都使用一个角色。 我想根据角色重定向到2个不同的站点,但框架始终重定向到最后一个defaultSuccessUrl("/...")**。这可能吗
我创建了一个基于 this tutorial 的小型网络应用程序。原始版本按预期工作。然后我做了一些更改,它停止工作,也就是说我可以在不登录的情况下访问/hello 页面。我正在学习这个神奇的自动配置
我有一个运行 Jersey Spring 调度程序的应用程序,我想与 Spring Security 集成。我可以让它与命名空间配置一起使用——即在 web.xml 中包含过滤器并使用 和 bean
过去几天我一直面临着 j_spring_security_check 的 404 错误,我想我能解决这个问题。我已经阅读了与此问题相关的几个堆栈溢出问题,但似乎没有什么可以改变结果。我希望有人能够发现
我尝试使用本教程通过 jwt token 创建身份验证:https://www.callicoder.com/spring-boot-spring-security-jwt-mysql-react-a
我在 web.xml 中遇到了这段代码,但我不明白如何添加 securityConfig.xml与 dispatcherContext.xml 一起添加到类路径 contextConfigLo
我正在为一个仍然使用 Acegi 插件(而不是较新的 Spring Core Security 插件)的项目编写测试用例,截至目前,我已经成功完成了本网站的工作(http://www.zorched.
在 webapp 本身中,一切运行都没有任何问题。身份验证按预期工作。但是,由于我的类路径上有 Spring Security,我所有的 Controller 测试现在都失败了,因为正在加载默认的 S
我正在使用 spring-boot (使用 JWT 和 spring security ),并且我在 spring-boot 中有一个类 configSecurity ,代码如下: @Configur
我已经有一个基于 XML 的 Spring 应用程序。我被要求将 Spring Security 集成到其中。我想使用基于 Java 的配置。我已经按照此处的说明对其进行了初始化:Java Confi
我正在尝试结合 Java-config 和 xml-config 进行 spring 安全认证。但是我收到一个错误: Error creating bean with name 'securityCo
'org.springframework.security.access.SecurityConfig':通过构造函数参数 0 表示的不满足的依赖关系:没有 [java.lang.String] 类型
我试图让 Spring SecuirtyConfig 与 Spring MVC 和 JavaConfig 一起工作,但我收到以下错误: java.lang.IllegalStateException:
奇怪的问题在这里。我的模板gsp中有一个sec:ifNotLogged in标记,它失败并显示以下错误/堆栈跟踪。但是,如果删除标签以便可以登录,然后在登录后再添加回标签,则可以正常工作。我有一个扩展
我正在做图书馆管理系统的 spring boot 安全应用。当我运行我的应用程序时,出现上述错误。有人可以帮帮我吗?提前致谢! //安全配置 @Configuration @EnableWebSecu
当我运行网络应用程序时,我遇到此错误: SEVERE: Context initialization failed org.springframework.beans.factory.BeanCrea
我是一名优秀的程序员,十分优秀!