Spring Security 5, Thymelead and login not working correctly

249
January 21, 2018, at 1:09 PM

I just "switched classes" - from PHP to Java because I'm much more familiar with java and want to start using it to develop webapps. Currently, I'm creating my base-project, with templating, logging and user management built in, so when I need to start a new project I have a common codebase to start from.

However, I'm really struggling to get user management / sessions working - here is what I've done so far:

Over the last three days, I trief gigguring out what I'm doing wrong, and how to get authentication working with spring and thymeleaf and I'm getting really frustrated - all I want is for users to be able to log in - authenticated against a database - can't imagine that it should be that hard - what am I doing wrong?

SecurityConfig:

@Configuration
@ComponentScan("ch.ucreek.alesia")
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled=true, prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
private final Logger logger = LogManager.getLogger(SecurityConfig.class);
@Autowired
protected void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
            .withUser("admin").password("Einfach1").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
    logger.error("configureHttpAuth called");
    http.authorizeRequests().anyRequest().authenticated()
            .and()
            .logout()
            .logoutSuccessUrl("/login")
            .permitAll();
    logger.error("configureHttpAuth finished");
}
}

WebMvcConfig:

@Configuration
@ComponentScan("ch.ucreek.alesia")
@EnableWebMvc
@PropertySource("classpath:application.properties")
public class WebMvcConfig implements WebMvcConfigurer {
private final Logger logger = LogManager.getLogger(WebMvcConfig.class);
@Autowired
private Environment env;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    logger.debug("addResourceHandlers");
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Bean
public ReloadableResourceBundleMessageSource messageSource(){
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("classpath:i18n/texts");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}
@Bean
public CookieLocaleResolver localeResolver(){
    CookieLocaleResolver localeResolver = new CookieLocaleResolver();
    localeResolver.setDefaultLocale(Locale.ENGLISH);
    localeResolver.setCookieName("my-locale-cookie");
    localeResolver.setCookieMaxAge(3600);
    return localeResolver;
}
@Bean
public LocaleChangeInterceptor localeInterceptor() {
    LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
    interceptor.setParamName("lang");
    return interceptor;
}
@Bean public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeInterceptor());
    registry.addInterceptor(new TrailingSlashInterceptor());
    logger.debug("addedInterceptor TrailingSlashInterceptor");
}
@Bean
@Description("Thymeleaf View Resolver")
public ThymeleafViewResolver viewResolver() {
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(templateEngine());
    viewResolver.setOrder(1);
    return viewResolver;
}
@Bean
@Description("Thymeleaf Template Resolver")
public ITemplateResolver templateResolver() {
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    templateResolver.setPrefix("/WEB-INF/views/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML5");
    return templateResolver;
}
@Bean
@Description("Thymeleaf Template Engine")
public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver());
    //templateEngine.setTemplateEngineMessageSource(messageSource());
    templateEngine.addDialect(new LayoutDialect());
    templateEngine.addDialect(new SpringSecurityDialect());
    return templateEngine;
}
@Bean(name = "dataSource")
public DataSource dataSource() {
    final DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
    dataSource.setUrl(env.getProperty("jdbc.url"));
    dataSource.setUsername(env.getProperty("jdbc.user"));
    dataSource.setPassword(env.getProperty("jdbc.pass"));
    return dataSource;
}
}

WebMvcInitializer:

@ComponentScan("ch.ucreek.alesia")
public class WebMvcInitializer implements WebApplicationInitializer {
@Override
public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(WebMvcConfig.class);
    ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
    registration.addMapping("/");
    registration.setLoadOnStartup(1);
}
}
Rent Charter Buses Company
READ ALSO
Sorting in descending order in java shows one element less [on hold]

Sorting in descending order in java shows one element less [on hold]

I wrote a code to sort an array of 50 elements on descending order but the array that shows on the console has 49 elementsCan anyone help me with this?

235
Can't set constraints programmatically after inflating the view

Can't set constraints programmatically after inflating the view

I am inflating a layout and I'm trying to set the constraints of the view objects after inflationHere is the code:

217
javac responds with “not a file” but the file exists

javac responds with “not a file” but the file exists

When I compile one Java file with javac, it responds with the error not a file (instead of file not found):

224