Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Configuration
- @EnableWebMvcSecurity
- @EnableGlobalMethodSecurity(
- prePostEnabled = true, order = 0, mode = AdviceMode.PROXY,
- proxyTargetClass = false
- )
- public class SecurityConfiguration extends WebSecurityConfigurerAdapter
- {
- @Inject UserService userService;
- @Bean
- protected SessionRegistry sessionRegistryImpl()
- {
- return new SessionRegistryImpl();
- }
- @Bean
- @Override
- public AuthenticationManager authenticationManagerBean() throws Exception
- {
- return super.authenticationManagerBean();
- }
- @Override
- protected void configure(AuthenticationManagerBuilder builder)
- throws Exception
- {
- builder
- .userDetailsService(this.userService)
- .passwordEncoder(new BCryptPasswordEncoder())
- .and()
- .eraseCredentials(true);
- }
- @Override
- public void configure(WebSecurity security)
- {
- security.ignoring().antMatchers("/resources/**", "/favicon.ico");
- }
- @Override
- protected void configure(HttpSecurity security) throws Exception
- {
- security
- .authorizeRequests()
- .antMatchers("/session/list")
- .hasAuthority("VIEW_USER_SESSIONS")
- .anyRequest().authenticated()
- .and().formLogin()
- .loginPage("/login").failureUrl("/login?loginFailed")
- .defaultSuccessUrl("/content/list")
- .usernameParameter("username")
- .passwordParameter("password")
- .permitAll()
- .and().logout()
- .logoutUrl("/logout").logoutSuccessUrl("/login?loggedOut")
- .invalidateHttpSession(true).deleteCookies("JSESSIONID")
- .permitAll()
- .and().sessionManagement()
- .sessionFixation().changeSessionId()
- .maximumSessions(1).maxSessionsPreventsLogin(true)
- .sessionRegistry(this.sessionRegistryImpl())
- .and().and().csrf()
- .requireCsrfProtectionMatcher((r) -> {
- String m = r.getMethod();
- return !r.getServletPath().startsWith("/services/") &&
- ("POST".equals(m) || "PUT".equals(m) ||
- "DELETE".equals(m) || "PATCH".equals(m));
- });
- }
- }
- @WebController
- public class AuthenticationController
- {
- @RequestMapping(value = "login", method = RequestMethod.GET)
- public ModelAndView login(Map<String, Object> model)
- {
- if(SecurityContextHolder.getContext().getAuthentication() instanceof
- UserPrincipal)
- return new ModelAndView(new RedirectView("/content/list", true, false));
- model.put("loginForm", new LoginForm());
- return new ModelAndView("login");
- }
- public static class LoginForm
- {
- private String username;
- private String password;
- public String getUsername()
- {
- return username;
- }
- public void setUsername(String username)
- {
- this.username = username;
- }
- public String getPassword()
- {
- return password;
- }
- public void setPassword(String password)
- {
- this.password = password;
- }
- }
- }
- @RequestMapping(value = {"", "list"}, method = RequestMethod.GET)
- public String list(Map<String, Object> model)
- {
- log.debug("Listing tickets.");
- model.put("tickets", this.ticketService.getAllTickets());
- return "content/list";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement