cleanup work (mostly formating)
This commit is contained in:
		
							parent
							
								
									f0b106ab60
								
							
						
					
					
						commit
						bb662b4252
					
				| @ -13,23 +13,21 @@ import org.springframework.web.servlet.ModelAndView; | |||||||
| @Controller | @Controller | ||||||
| public class MyController { | public class MyController { | ||||||
| 
 | 
 | ||||||
| 	/** |     /** | ||||||
| 	 * Build the view model for the login page (add authentication error |      * Build the view model for the login page (add authentication error | ||||||
| 	 * information in the event of an unsuccessful login attempt). |      * information in the event of an unsuccessful login attempt). | ||||||
| 	 */ |      */ | ||||||
| 	@RequestMapping(value = "/login", method = RequestMethod.GET) |     @RequestMapping(value = "/login", method = RequestMethod.GET) | ||||||
| 	public ModelAndView login( |     public ModelAndView login(@RequestParam(value = "error", required = false) String error) { | ||||||
| 			@RequestParam(value = "error", required = false) String error) { |  | ||||||
| 
 | 
 | ||||||
| 		ModelAndView model = new ModelAndView(); |         ModelAndView model = new ModelAndView(); | ||||||
| 		if (error != null) { |         if (error != null) { | ||||||
| 			model.addObject("message", |             model.addObject("message", "Username or password not recognised - please try again."); | ||||||
| 					"Username or password not recognised - please try again."); |         } | ||||||
| 		} |  | ||||||
| 
 | 
 | ||||||
| 		model.setViewName("login"); |         model.setViewName("login"); | ||||||
| 		return model; |         return model; | ||||||
| 
 | 
 | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -17,9 +17,9 @@ import org.springframework.security.web.WebAttributes; | |||||||
| import org.springframework.security.web.authentication.AuthenticationSuccessHandler; | import org.springframework.security.web.authentication.AuthenticationSuccessHandler; | ||||||
| import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||||
| 
 | 
 | ||||||
| @Component(value="mySimpleUrlAuthenticationSuccessHandler") | @Component(value = "mySimpleUrlAuthenticationSuccessHandler") | ||||||
| public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler { | public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler { | ||||||
| 	 | 
 | ||||||
|     private final Log logger = LogFactory.getLog(this.getClass()); |     private final Log logger = LogFactory.getLog(this.getClass()); | ||||||
| 
 | 
 | ||||||
|     private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); |     private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); | ||||||
|  | |||||||
| @ -6,7 +6,6 @@ package org.baeldung.security; | |||||||
|  */ |  */ | ||||||
| public enum SecurityRole { | public enum SecurityRole { | ||||||
| 
 | 
 | ||||||
| 	ROLE_USER, |     ROLE_USER, ROLE_ADMIN; | ||||||
| 	ROLE_ADMIN; | 
 | ||||||
| 	 |  | ||||||
| } | } | ||||||
|  | |||||||
| @ -6,6 +6,6 @@ import org.springframework.security.web.context.AbstractSecurityWebApplicationIn | |||||||
|  * Registers the springSecurityFilterChain Filter for every URL in the application.  |  * Registers the springSecurityFilterChain Filter for every URL in the application.  | ||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
| public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer{ | public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -23,65 +23,61 @@ import org.springframework.stereotype.Service; | |||||||
| @Service | @Service | ||||||
| public class MyUserDetailsService implements UserDetailsService { | public class MyUserDetailsService implements UserDetailsService { | ||||||
| 
 | 
 | ||||||
| 	private final Log logger = LogFactory.getLog(this.getClass()); |     private final Log logger = LogFactory.getLog(this.getClass()); | ||||||
| 
 | 
 | ||||||
| 	private Map<String, User> availableUsers = new HashMap<String, User>(); |     private Map<String, User> availableUsers = new HashMap<String, User>(); | ||||||
| 
 | 
 | ||||||
| 	public MyUserDetailsService() { |     public MyUserDetailsService() { | ||||||
| 
 | 
 | ||||||
| 		populateDemoUsers(); |         populateDemoUsers(); | ||||||
| 		 |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	@Override |     } | ||||||
| 	public UserDetails loadUserByUsername(String username) |  | ||||||
| 			throws UsernameNotFoundException { |  | ||||||
| 
 | 
 | ||||||
| 		logger.info("Load user by username " + username); |     @Override | ||||||
|  |     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||||||
| 
 | 
 | ||||||
| 		UserDetails user = availableUsers.get(username); |         logger.info("Load user by username " + username); | ||||||
| 		if (user == null) { |  | ||||||
| 			throw new UsernameNotFoundException("Username not found"); |  | ||||||
| 		} else { |  | ||||||
| 			return availableUsers.get(username); |  | ||||||
| 		} |  | ||||||
| 
 | 
 | ||||||
| 	} |         UserDetails user = availableUsers.get(username); | ||||||
|  |         if (user == null) { | ||||||
|  |             throw new UsernameNotFoundException("Username not found"); | ||||||
|  |         } else { | ||||||
|  |             return availableUsers.get(username); | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
| 	/** |     } | ||||||
| 	 * Create demo users (note: obviously in a real system these would be persisted |  | ||||||
| 	 * in database or retrieved from another system). |  | ||||||
| 	 */ |  | ||||||
| 	private void populateDemoUsers(){ |  | ||||||
| 		 |  | ||||||
| 		logger.info("Populate demo users"); |  | ||||||
| 		 |  | ||||||
| 		availableUsers.put("user", |  | ||||||
| 				createUser("user", "password", Arrays.asList(SecurityRole.ROLE_USER))); |  | ||||||
| 		availableUsers.put("admin", |  | ||||||
| 				createUser("admin", "password", Arrays.asList(SecurityRole.ROLE_ADMIN))); |  | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	 |  | ||||||
| 	/** |  | ||||||
| 	 * Create a demo User. |  | ||||||
| 	 *  |  | ||||||
| 	 * @param username |  | ||||||
| 	 *            Username |  | ||||||
| 	 * @param password |  | ||||||
| 	 *            Password |  | ||||||
| 	 * @param roles |  | ||||||
| 	 *            Role names user is assigned to |  | ||||||
| 	 * @return User |  | ||||||
| 	 */ |  | ||||||
| 	private User createUser(String username, String password, List<SecurityRole> roles) { |  | ||||||
| 
 | 
 | ||||||
| 		logger.info("Create user " + username); |     /** | ||||||
|  |      * Create demo users (note: obviously in a real system these would be persisted | ||||||
|  |      * in database or retrieved from another system). | ||||||
|  |      */ | ||||||
|  |     private void populateDemoUsers() { | ||||||
| 
 | 
 | ||||||
| 		List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); |         logger.info("Populate demo users"); | ||||||
| 		for (SecurityRole role : roles) { | 
 | ||||||
| 			authorities.add(new SimpleGrantedAuthority(role.toString())); |         availableUsers.put("user", createUser("user", "password", Arrays.asList(SecurityRole.ROLE_USER))); | ||||||
| 		} |         availableUsers.put("admin", createUser("admin", "password", Arrays.asList(SecurityRole.ROLE_ADMIN))); | ||||||
| 		return new User(username, password, true, true, true, true, authorities); |     } | ||||||
| 	} | 
 | ||||||
|  |     /** | ||||||
|  |      * Create a demo User. | ||||||
|  |      *  | ||||||
|  |      * @param username | ||||||
|  |      *            Username | ||||||
|  |      * @param password | ||||||
|  |      *            Password | ||||||
|  |      * @param roles | ||||||
|  |      *            Role names user is assigned to | ||||||
|  |      * @return User | ||||||
|  |      */ | ||||||
|  |     private User createUser(String username, String password, List<SecurityRole> roles) { | ||||||
|  | 
 | ||||||
|  |         logger.info("Create user " + username); | ||||||
|  | 
 | ||||||
|  |         List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); | ||||||
|  |         for (SecurityRole role : roles) { | ||||||
|  |             authorities.add(new SimpleGrantedAuthority(role.toString())); | ||||||
|  |         } | ||||||
|  |         return new User(username, password, true, true, true, true, authorities); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -20,10 +20,10 @@ import com.google.common.base.Preconditions; | |||||||
| @PropertySource({ "classpath:persistence-h2.properties" }) | @PropertySource({ "classpath:persistence-h2.properties" }) | ||||||
| public class DatabaseConfig { | public class DatabaseConfig { | ||||||
| 
 | 
 | ||||||
| 	@Autowired |     @Autowired | ||||||
|     private Environment env; |     private Environment env; | ||||||
| 
 | 
 | ||||||
| 	@Bean |     @Bean | ||||||
|     public DataSource dataSource() { |     public DataSource dataSource() { | ||||||
|         final DriverManagerDataSource dataSource = new DriverManagerDataSource(); |         final DriverManagerDataSource dataSource = new DriverManagerDataSource(); | ||||||
|         dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName"))); |         dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName"))); | ||||||
|  | |||||||
| @ -15,13 +15,11 @@ import org.springframework.security.web.authentication.AuthenticationSuccessHand | |||||||
| @ImportResource({ "classpath:webSecurityConfig.xml" }) | @ImportResource({ "classpath:webSecurityConfig.xml" }) | ||||||
| public class SecurityConfig extends WebSecurityConfigurerAdapter { | public class SecurityConfig extends WebSecurityConfigurerAdapter { | ||||||
| 
 | 
 | ||||||
| 	@Autowired |     @Autowired | ||||||
| 	private AuthenticationSuccessHandler mySimpleUrlAuthenticationSuccessHandler; |     private AuthenticationSuccessHandler mySimpleUrlAuthenticationSuccessHandler; | ||||||
|  | 
 | ||||||
|  |     public SecurityConfig() { | ||||||
|  |         super(); | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
| 	public SecurityConfig() { |  | ||||||
| 		super(); |  | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| } | } | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user