使用Spring Boot配置Spring Security

9
我是一名新手,在使用Java配置配置Spring Security方面。我试图遵循{{link1:此帖子}}。然而,当我运行我的应用程序时,所有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的信息。这个摘要是打错了吗? - Dave Syer
我对Spring Boot的理解是,您会加入Actuator以进行安全控制,然后添加适当的Spring Java配置类。我的假设是否正确?我找到的唯一一篇Spring Boot与Spring Security的演练没有使用控制器,我尝试尽可能地模仿它在做什么。 - CodeChimp
你的理解是正确的。有两个示例(spring-boot-sample-secure是较新的,而spring-boot-sample-actuator是较旧的),它们都使用了Spring Security。"secure"这个示例可能最接近你的用例。 - Dave Syer
我在你发布的代码中仍然没有看到Spring Boot。如果我是你,我不会使用AbstractAnnotationConfigDispatcherServletInitializer,而是尝试使用SpringBootServletInitializer - Dave Syer
谢谢您的输入。我回家后会查看另一个样例。我可能在查看旧版GIT存储库。此外,我将切换到SpringBootServletInitializer。希望这能让我克服当前的障碍。 - CodeChimp
1个回答

6

最好的回答原问题是描述可用选项。一些应用程序(只需要基本的HTTP身份验证的服务)可以使用执行器中的默认设置,其他应用程序则需要指定security.*属性(请参见SecurityProperties以获取选项)和/或AuthenticationManager(用于用户帐户详细信息)。更高级别的控制来自添加自己的WebSecurityConfigurerAdapter,您可以通过查看Spring Boot中的“secure”示例了解如何执行该操作。


我按照Spring Boot中给出的“安全”示例进行操作,但是我无法覆盖默认安全密码的authenticationManager。即使我复制了ApplicationSecurity.class并重写了configure(authenticationManagerBuilder)方法,它仍然读取默认安全密码。 - SooCheng Koh
我发现我的问题是由https://dev59.com/-oTba4cB1Zd3GeqP97Vg#K5oToYgBc1ULPQZF7tyh引起的。 - SooCheng Koh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接