Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have to make your own custom authentication-provider.</p> <p><strong>Example code:</strong></p> <p><em>Service to load Users from Hibernate:</em></p> <pre><code>import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; @Service("userDetailsService") public class UserDetailsServiceImpl implements UserDetailsService { @Autowired private UserDao dao; @Autowired private Assembler assembler; @Transactional(readOnly = true) public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException { UserDetails userDetails = null; UserEntity userEntity = dao.findByName(username); if (userEntity == null) throw new UsernameNotFoundException("user not found"); return assembler.buildUserFromUserEntity(userEntity); } } </code></pre> <p><em>Service to convert your entity to a spring user object:</em></p> <pre><code>import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.GrantedAuthorityImpl; import org.springframework.security.core.userdetails.User; @Service("assembler") public class Assembler { @Transactional(readOnly = true) User buildUserFromUserEntity(UserEntity userEntity) { String username = userEntity.getName(); String password = userEntity.getPassword(); boolean enabled = userEntity.isActive(); boolean accountNonExpired = userEntity.isActive(); boolean credentialsNonExpired = userEntity.isActive(); boolean accountNonLocked = userEntity.isActive(); Collection&lt;GrantedAuthority&gt; authorities = new ArrayList&lt;GrantedAuthority&gt;(); for (SecurityRoleEntity role : userEntity.getRoles()) { authorities.add(new GrantedAuthorityImpl(role.getRoleName())); } User user = new User(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities, id); return user; } } </code></pre> <p><em>The namespace-based application-context-security.xml would look something like:</em></p> <pre><code>&lt;http&gt; &lt;intercept-url pattern="/login.do*" filters="none"/&gt; &lt;intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" /&gt; &lt;form-login login-page="/login.do" authentication-failure-url="/login.do?error=failed" login-processing-url="/login-please.do" /&gt; &lt;logout logout-url="/logoff-please.do" logout-success-url="/logoff.html" /&gt; &lt;/http&gt; &lt;beans:bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"&gt; &lt;beans:property name="userDetailsService" ref="userDetailsService"/&gt; &lt;/beans:bean&gt; &lt;beans:bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager"&gt; &lt;beans:property name="providers"&gt; &lt;beans:list&gt; &lt;beans:ref local="daoAuthenticationProvider" /&gt; &lt;/beans:list&gt; &lt;/beans:property&gt; &lt;/beans:bean&gt; &lt;authentication-manager&gt; &lt;authentication-provider user-service-ref="userDetailsService"&gt; &lt;password-encoder hash="md5"/&gt; &lt;/authentication-provider&gt; &lt;/authentication-manager&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload