【发布时间】:2014-01-28 20:23:23
【问题描述】:
我在/src/main/java/com/dog/bootstrap中放置了以下配置:
@EnableWebSecurity
@Configuration
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
System.out.println("hello");
auth.inMemoryAuthentication()
.withUser("user")
.password("password")
.roles("USER");
}
}
我正在按如下方式加载它:
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.scan("com.dog.bootstrap");
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(dispatcherContext));
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
Set<String> mappingConflicts = dispatcher.addMapping("/");
if (!mappingConflicts.isEmpty()) {
throw new IllegalStateException("'dispatcher' could not be mapped to '/' due " +
"to an existing mapping.");
}
}
我的控制器:
@Controller
public class DogController {
@RequestMapping(value = {"/dog"}, method = RequestMethod.GET)
@ResponseBody
public String getSource(@PathVariable("domain") String domain) throws Exception {
return "dogs";
}
}
当我启动我的应用程序时,我确实看到 hello 正在打印,所以 configure(AuthenticationManagerBuilder auth) 正在被调用。但是,我的所有端点都不需要我输入登录页面。当我转到localhost:8080/dog 时,它会输出dogs 而不会要求我进行身份验证。
【问题讨论】:
标签: java spring authentication spring-security restful-authentication