【问题标题】:Spring boot ServeletInitializer and Spring SecuritySpring Boot ServeletInitializer 和 Spring Security
【发布时间】:2015-06-09 06:13:09
【问题描述】:

我有 2 个配置文件。一是Spring Boot应用程序

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
    ...
   }

还有 Spring 安全配置。看来它不起作用。每当我访问 localhost:8080 时,它都会询问我的用户名和密码。我相信我在auth.inMemoryAuthentication().withUser("user").password("password").roles("USER") 中配置了

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter{
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
            .withUser("user").password("password").roles("USER");
    }
}

但它显示的凭据无效,是否有任何验证方法?

编辑:我正在尝试将此 xml 配置转换为基于 JavaConfig 但仍然无济于事。

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security.xsd
           http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="org.app.genesis.client.auth"/>

    <http pattern="/resources/**" security="none"/>
    <http pattern="/index.jsp" security="none"/>

    <http>
        <intercept-url pattern="/api/*" requires-channel="https"/>
        <!--TODO Add RESOURCE PATTERN checker -->
        <form-login login-page="/index.jsp" default-target-url="/dashboard"/>
        <logout />
    </http>

    <!-- Test Login values -->
    <authentication-manager>
        <!--use inMemoryUserDetailsService for faux auth -->
        <authentication-provider ref="customAuthenticationProvider"/>
    </authentication-manager>
</beans:beans>

这是我的新安全配置

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private TenantDetailsService tenantUserDetailsService;

    @Autowired
    private PasswordEncryptionService passwordEncoder;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(tenantUserDetailsService).passwordEncoder(passwordEncoder);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.formLogin().loginPage("/index.jsp").defaultSuccessUrl("/dashboard");
    }
}

安全配置.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security.xsd
           http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="org.brightworks.genesis.client.auth"/>

    <http pattern="/resources/**" security="none"/>
    <http pattern="/index.jsp" security="none"/>

    <http>
        <intercept-url pattern="/api/*" requires-channel="https"/>
        <!--TODO Add RESOURCE PATTERN checker -->
        <form-login login-page="/index.jsp" default-target-url="/dashboard"/>
        <logout />
    </http>

    <!-- Test Login values -->
    <authentication-manager>
        <!--use inMemoryUserDetailsService for faux auth -->
        <authentication-provider ref="customAuthenticationProvider"/>
    </authentication-manager>
</beans:beans>

【问题讨论】:

  • 您使用的是什么版本的 Spring Boot(它适用于我的 1.2.3.RELEASE)?
  • 我也在使用 Spring boot 1.2.3.RELEASE。 Spring boot 使用默认的 Spring Security Authentication
  • 更新配置的一个版本也适用于我。我认为你必须描述你做了什么以及发生了什么。
  • @DaveSyer 我真正想做的是将我的 spring-security 配置迁移到基于 Annotation 的 java 配置,但是,我失败了。你能帮忙把这个xml转换成基于java的配置吗?我已经添加了我的 security-config.xml
  • 你还没有真正说出“我失败了”的意思。如果您说您要测试它以及出了什么问题,那将有所帮助。

标签: java spring spring-mvc spring-security spring-boot


【解决方案1】:

如果您想使用自己的身份验证版本。您首先禁用弹簧靴弹簧安全配置。将此添加到您的 application.properties。

security.basic.enabled=false

并将您的 http 配置更改为此。

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/**")
            .hasAnyRole("ROLE1","ROLE2")
            .and()
            .formLogin()
            .loginPage("/login")
            .loginProcessingUrl("/j_spring_security_check")
            .defaultSuccessUrl("/product/search", true)
            .permitAll()
            .and()
            .csrf()
            .disable()
            .logout()
            .logoutUrl("/j_spring_security_logout")
            .logoutSuccessUrl("/login");
    }

将上述配置与此登录表单匹配

<form class="form-signin"name="f" action="${pageContext.request.contextPath}/j_spring_security_check" method="POST">
    <fieldset>
            <input class="form-control form-group" type="text" name="username" placeholder="Username">
            <input class="form-control" type="password" name="password" placeholder="Password" >
            <a class="forgot pull-right" href="#">Forgot password?</a>
            <button name="submit" class="btn btn-block btn-primary" type="submit">Sign in</button>
    </fieldset>
</form>

假设页面登录页面是“/login”,您所说的 POST 请求是 j_spring_security_check。因此,loginProcessingUrl 设置为

j_spring_security_check

【讨论】:

    【解决方案2】:

    使用 auth.inMemoryAuthentication() 您只需定义用户及其凭据。 如果你想使用这些,你必须告诉 Spring Boot 不要创建自己的默认值。 Spring Boot 的默认值是“用户”和运行应用程序时在控制台中显示的密码。 您可以在 application.properties 文件中设置自己的默认凭据,如下所示:

    security.user.name=user
    security.user.password=password
    management.security.role=USER
    

    【讨论】:

    • 如何告诉spring boot不要使用它的默认值?
    • 你应该在你的类中定义一个 configure(HttpSecurity http) 方法来扩展 Spring Security 类 WebSecurityConfigurerAdapter,正如 Spring Boot 参考文档的第 27 章(上面的链接)告诉你的那样。 github.com/spring-projects/spring-boot/tree/v1.2.3.RELEASE/… 的示例,特别是类文件 SampleWebSecureApplication.java 以及属性文件 application.properties 展示了如何配置表单登录和覆盖,在本例中为登录密码。
    • 谢谢!我已经更新了我的 SecurityConfig,但是,在添加凭据后,它提示发布不支持且不允许的帖子。我错过了什么。我也更新了我的问题。
    • @user962206,不过要说一句:Spring 不再使用 /j_spring_security_check。 Spring 使用 /login 代替。默认。您可以将其更改为最适合您的套件。请参考this post
    猜你喜欢
    • 2019-07-16
    • 1970-01-01
    • 2014-07-25
    • 2017-09-11
    • 2019-03-07
    • 2015-09-03
    • 1970-01-01
    • 2018-01-29
    • 2017-07-22
    相关资源
    最近更新 更多