Konfiguration von Spring Security mit Spring Boot

Ich bin neu konfigurieren Spring Security Verwendung Java-Config. Ich versuchte, zu Folgen dieses posting. Jedoch, wenn ich meine app, bekomme ich eine Basic Auth Herausforderung auf alle URLs, einschließlich /. Die Eingabe, die entweder der user-id/pass-combos, die unten nicht zu funktionieren scheint.

Mein Controller:

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";
    }
}

Nicht sicher, was anderes zu versuchen. Gerade auf der Suche für einige Hinweise, warum dies nicht funktioniert.

Update

Vollständigkeit halber, hier ist die config-Klasse:

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
    }
}

Und die 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;
    }

}

Ich nicht diese vor, weil Sie ziemlich viel ein copy-paste aus der referenzierten blog-posting.

  • Ich sehe nicht, Spring-Boot-überall da. Ist die Zusammenfassung ein Tippfehler?
  • Mein Verständnis mit Spring Boot ist, dass Sie setzen würde, den Antrieb für die Sicherheit, dann fügen Sie die passenden Spring-Java-Klassen-Konfiguration Platz. Bin ich falsch in dieser Annahme? Die einzige Komplettlösung die ich gefunden habe, für Spring Boot mit Spring Security nicht mit einem Controller, und ich versuchte, das zu kopieren, was es war, so nah wie ich konnte.
  • Ihr Verständnis ist korrekt. Es gibt zwei Proben (spring-boot-sample-secure, die ist newish und spring-boot-sample-Antrieb, das älter ist) mit Spring Security. Das "sicher" ist wahrscheinlich am nächsten zu Ihrem Anwendungsfall.
  • (Ich verstehe immer noch nicht, Spring Boot irgendwo in dem code, den Sie geschrieben obwohl. Ich würde mich nicht mit AbstractAnnotationConfigDispatcherServletInitializer zum Beispiel, wenn ich du wäre - versuchen SpringBootServletInitializer).
  • Vielen Dank für den input. Will check out die andere Probe, wenn ich nach Hause komme. Ich bin wahrscheinlich auf der Suche auf einen älteren GIT repo. Auch wechselt die SpringBootServletInitializer. Hoffentlich bekommt der von mir über meine aktuelle Straße sperren.
InformationsquelleAutor CodeChimp | 2013-10-23
Schreibe einen Kommentar