如何在spring boot中登录用户名?

时间:2015-02-15 08:05:49

标签: spring-security spring-boot

我正在开发一个使用spring boot with spring security的项目,下面是我的安全配置

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource datasource;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
            .csrf().disable();


        http
            .authorizeRequests()
            .antMatchers("/ideate","/homecreate").permitAll()
                .anyRequest().authenticated();  

        http
                .authorizeRequests().anyRequest().authenticated();
        http
                .formLogin().failureUrl("/login?error")
                .defaultSuccessUrl("/")
                .loginPage("/login")
                .permitAll()
                .and()
                .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login")
                .permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        JdbcUserDetailsManager userDetailsService = new JdbcUserDetailsManager();

        userDetailsService.setDataSource(datasource);
        PasswordEncoder encoder = new BCryptPasswordEncoder();

        auth.userDetailsService(userDetailsService).passwordEncoder(encoder);
        auth.jdbcAuthentication().dataSource(datasource);


    }
}

成功登录用户后,我正在调用请求映射" /"在控制器中写入,该方法将返回一个页面,该页面应包含用户名作为模型属性如何获取当前登录的用户名?

1 个答案:

答案 0 :(得分:0)

在jsp中使用以下代码......

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
 <sec:authentication property="principal.username" />

这段代码帮助我将用户信息提取到jsp。