cleanup work
This commit is contained in:
@@ -25,7 +25,7 @@ public class MyUserDetailsService implements UserDetailsService {
|
||||
|
||||
private final Log logger = LogFactory.getLog(this.getClass());
|
||||
|
||||
private Map<String, User> availableUsers = new HashMap<String, User>();
|
||||
private final Map<String, User> availableUsers = new HashMap<String, User>();
|
||||
|
||||
public MyUserDetailsService() {
|
||||
|
||||
@@ -33,12 +33,13 @@ public class MyUserDetailsService implements UserDetailsService {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
//
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {
|
||||
logger.info("Load user by username " + username);
|
||||
|
||||
UserDetails user = availableUsers.get(username);
|
||||
final UserDetails user = availableUsers.get(username);
|
||||
if (user == null) {
|
||||
throw new UsernameNotFoundException("Username not found");
|
||||
} else {
|
||||
@@ -52,7 +53,6 @@ public class MyUserDetailsService implements UserDetailsService {
|
||||
* 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)));
|
||||
@@ -70,12 +70,11 @@ public class MyUserDetailsService implements UserDetailsService {
|
||||
* Role names user is assigned to
|
||||
* @return User
|
||||
*/
|
||||
private User createUser(String username, String password, List<SecurityRole> roles) {
|
||||
|
||||
private User createUser(final String username, final String password, final List<SecurityRole> roles) {
|
||||
logger.info("Create user " + username);
|
||||
|
||||
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
|
||||
for (SecurityRole role : roles) {
|
||||
final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
|
||||
for (final SecurityRole role : roles) {
|
||||
authorities.add(new SimpleGrantedAuthority(role.toString()));
|
||||
}
|
||||
return new User(username, password, true, true, true, true, authorities);
|
||||
|
||||
@@ -18,11 +18,13 @@ import com.google.common.base.Preconditions;
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@PropertySource({ "classpath:persistence-h2.properties" })
|
||||
public class DatabaseConfig {
|
||||
public class PersistenceConfig {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
//
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
@@ -32,4 +34,5 @@ public class DatabaseConfig {
|
||||
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
package org.baeldung.spring;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||
|
||||
/**
|
||||
* Spring Security Configuration.
|
||||
@@ -15,9 +13,6 @@ import org.springframework.security.web.authentication.AuthenticationSuccessHand
|
||||
@ImportResource({ "classpath:webSecurityConfig.xml" })
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Autowired
|
||||
private AuthenticationSuccessHandler mySimpleUrlAuthenticationSuccessHandler;
|
||||
|
||||
public SecurityConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
@@ -1,55 +1,51 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/security"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
|
||||
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd">
|
||||
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd"
|
||||
>
|
||||
|
||||
|
||||
<http use-expressions="true">
|
||||
|
||||
<intercept-url pattern="/anonymous*" access="isAnonymous()" />
|
||||
<intercept-url pattern="/login*" access="permitAll" />
|
||||
<intercept-url pattern="/**" access="isAuthenticated()" />
|
||||
<intercept-url pattern="/anonymous*" access="isAnonymous()"/>
|
||||
<intercept-url pattern="/login*" access="permitAll"/>
|
||||
<intercept-url pattern="/**" access="isAuthenticated()"/>
|
||||
|
||||
<form-login login-page='/login.html' authentication-success-handler-ref="mySimpleUrlAuthenticationSuccessHandler" authentication-failure-url="/login.html?error=true" />
|
||||
<form-login login-page='/login.html' authentication-success-handler-ref="mySimpleUrlAuthenticationSuccessHandler" authentication-failure-url="/login.html?error=true"/>
|
||||
|
||||
<logout delete-cookies="JSESSIONID" />
|
||||
<logout delete-cookies="JSESSIONID"/>
|
||||
|
||||
<remember-me data-source-ref="dataSource" token-validity-seconds="86400"/>
|
||||
<remember-me data-source-ref="dataSource" token-validity-seconds="86400"/>
|
||||
|
||||
</http>
|
||||
|
||||
|
||||
<!-- create H2 embedded database table on startup -->
|
||||
<jdbc:embedded-database id="dataSource" type="H2">
|
||||
<jdbc:script location="classpath:/persisted_logins_create_table.sql"/>
|
||||
</jdbc:embedded-database>
|
||||
<jdbc:script location="classpath:/persisted_logins_create_table.sql"/>
|
||||
</jdbc:embedded-database>
|
||||
|
||||
<!-- Persistent Remember Me Service -->
|
||||
<beans:bean id="rememberMeAuthenticationProvider" class="org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices">
|
||||
<beans:constructor-arg value="myAppKey" />
|
||||
<beans:constructor-arg ref="jdbcTokenRepository" />
|
||||
<beans:constructor-arg ref="myUserDetailsService" />
|
||||
</beans:bean>
|
||||
|
||||
<!-- Uses a database table to maintain a set of persistent login data -->
|
||||
<beans:bean id="jdbcTokenRepository" class="org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl">
|
||||
<beans:property name="createTableOnStartup" value="false" />
|
||||
<beans:property name="dataSource" ref="dataSource" />
|
||||
</beans:bean>
|
||||
|
||||
<!-- Authentication Manager (uses same UserDetailsService as RememberMeService)-->
|
||||
<authentication-manager alias="authenticationManager">
|
||||
<authentication-provider user-service-ref="myUserDetailsService">
|
||||
</authentication-provider>
|
||||
</authentication-manager>
|
||||
<!-- Persistent Remember Me Service -->
|
||||
<beans:bean id="rememberMeAuthenticationProvider" class="org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices">
|
||||
<beans:constructor-arg value="myAppKey"/>
|
||||
<beans:constructor-arg ref="jdbcTokenRepository"/>
|
||||
<beans:constructor-arg ref="myUserDetailsService"/>
|
||||
</beans:bean>
|
||||
|
||||
<!-- Uses a database table to maintain a set of persistent login data -->
|
||||
<beans:bean id="jdbcTokenRepository" class="org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl">
|
||||
<beans:property name="createTableOnStartup" value="false"/>
|
||||
<beans:property name="dataSource" ref="dataSource"/>
|
||||
</beans:bean>
|
||||
|
||||
<!-- Authentication Manager (uses same UserDetailsService as RememberMeService) -->
|
||||
<authentication-manager alias="authenticationManager">
|
||||
<authentication-provider user-service-ref="myUserDetailsService">
|
||||
</authentication-provider>
|
||||
</authentication-manager>
|
||||
|
||||
</beans:beans>
|
||||
Reference in New Issue
Block a user