3

I am developing a project using spring boot with spring security, below is my security configuration

@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);


    }
}

After successful Log in of the user, i am calling a request mapping "/" written in the controller, the method will return a page which should contain username as model attribute how do i get the current logged in username?

Manikanta
  • 3,421
  • 4
  • 30
  • 51
  • 1
    duplicate: http://stackoverflow.com/questions/248562/when-using-spring-security-what-is-the-proper-way-to-obtain-current-username-i – ikumen Feb 15 '15 at 08:16
  • This is in spring boot where we don't have any XML configurations , does authentication or principal object provide the user name? – Manikanta Feb 15 '15 at 08:32
  • Did you try it and it didn't? Also, note there is no XML in the accepted answer to the duplicate post (and Spring Boot works with XML anyway if you want to use it). – Dave Syer Feb 15 '15 at 22:20

1 Answers1

0

Use the below code in your jsp ...

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

This bit of code helped me to fetch the user information to the jsp.

vamsi
  • 1,219
  • 1
  • 9
  • 15