【发布时间】:2013-11-01 02:02:05
【问题描述】:
我是使用 Java Config 配置 Spring Security 的新手。我试图关注this posting。但是,当我运行我的应用程序时,我会在所有 URL(包括 /)上收到基本身份验证挑战。输入以下任一用户 ID/密码组合似乎都不起作用。
我的控制器:
package com.xxx.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/")
/**
* Controller to handle basic "root" URLs
*
* @author xxx
* @version 0.1.0
*/
public class RootController {
/**
* Handles '/'
* @param model
* @return
*/
@RequestMapping
public String index(Model model) {
return "index";
}
/**
* Handles '/signup'
* @param model
* @return
*/
@RequestMapping("/signup")
public String signup(Model model) {
return "signup";
}
/**
* Handles '/about'
* @param model
* @return
*/
@RequestMapping("/about")
public String about(Model model) {
return "about";
}
/**
* Handles '/login'
* @param model
* @return
*/
@RequestMapping("/login")
public String login(Model model) {
return "login";
}
/**
* Handles '/admin'
* @param model
* @return
*/
@RequestMapping("/admin")
public String admin(Model model) {
return "admin";
}
}
不知道还有什么可以尝试的。只是寻找一些关于为什么这不起作用的指导。
更新
为了完整起见,这里是配置类:
package com.xxx.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
/**
* Configures the security for the application
*
* @author XXX
* @version 0.1.0
*
*/
public class WebSecurityAppConfig extends WebSecurityConfigurerAdapter {
@Override
/**
* @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#registerAuthentication(AuthenticationManagerBuilder)
*/
protected void registerAuthentication(AuthenticationManagerBuilder auth)
throws Exception {
auth
.inMemoryAuthentication()
.withUser("user") // #1
.password("password")
.roles("USER")
.and()
.withUser("admin") // #2
.password("password")
.roles("ADMIN","USER");
}
@Override
/**
* @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(WebSecurity)
*/
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**"); // #3
}
@Override
/**
* @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(HttpSecurity)
*/
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/","/signup","/about").permitAll() // #4
.antMatchers("/admin/**").hasRole("ADMIN") // #6
.anyRequest().authenticated() // 7
.and()
.formLogin() // #8
.loginPage("/login") // #9
.permitAll(); // #5
}
}
还有 WebApplicationInitializer:
package com.xxx.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
*
* @author XXX
* @version 0.1.0
*/
public class SpringWebMvcInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
/**
*
*/
protected Class<?>[] getRootConfigClasses() {
return new Class[] { WebSecurityAppConfig.class };
}
@Override
/**
*
*/
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
/**
*
*/
protected String[] getServletMappings() {
return null;
}
}
我之前没有包含这些内容,因为它们几乎是引用的博客帖子的复制粘贴。
【问题讨论】:
-
我在那里看不到 Spring Boot。总结是错字吗?
-
我对 Spring Boot 的理解是,您可以将 Actuator 放入 Security,然后添加适当的 Spring Java 配置类。我在这个假设上错了吗?我发现的 Spring Boot 与 Spring Security 的唯一演练不使用控制器,我试图尽可能接近地复制它正在做的事情。
-
你的理解是正确的。有两个使用 Spring Security 的示例(新的 spring-boot-sample-secure 和较旧的 spring-boot-sample-actuator)。 “安全”的可能最接近您的用例。
-
(我仍然没有在您发布的代码中的任何地方看到 Spring Boot。例如,如果我是您,我不会使用
AbstractAnnotationConfigDispatcherServletInitializer- 试试SpringBootServletInitializer)。 -
感谢您的意见。我回家后会检查另一个样品。我可能正在查看一个较旧的 GIT 存储库。此外,将切换到
SpringBootServletInitializer。希望这能让我克服目前的障碍。
标签: spring spring-mvc spring-security spring-boot spring-java-config