Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think that the best way to do this is to implement UserDetailsService and UserDetails.</p> <p>Example implementation of UserDetailsService.</p> <pre><code>@Service @Transactional public class UserLoginService implements UserDetailsService { @Autowired private UserService userService; @Override public UserDetails loadUserByUsername(String userId) throws UsernameNotFoundException { UserEntity userEntity = this.userService.getUserByUserId(userId); if (userEntity == null) { throw new UsernameNotFoundException("User not found"); } UserLoginBean bean = new UserLoginBean(userEntity.getId(), userEntity.getUserId(), userEntity.getPassword(), userEntity.getEnabled()); bean.setFullname(userEntity.getFullname()); bean.setUserEntity(userEntity); Set&lt;GrantedAuthority&gt; roles = new HashSet&lt;GrantedAuthority&gt;(); roles.add( new SimpleGrantedAuthority( userEntity.getRole() ) ); bean.setAuthorities(roles); return bean; } } </code></pre> <p>Example implementation of UserDetails.</p> <pre><code>public class UserLoginBean implements UserDetails { private static final long serialVersionUID = 1L; private Long id; private String fullname; private String username; private String password; private boolean locked; private Set&lt;GrantedAuthority&gt; authorities = null; private UserEntity userEntity; public UserLoginBean(Long id, String username, String password, boolean locked ) { this.id = id; this.username = username; this.password = password; this.locked = locked; } public boolean isAccountNonExpired() { return true; } public boolean isAccountNonLocked() { return locked; } public boolean isCredentialsNonExpired() { return true; } public boolean isEnabled() { return true; } public Set&lt;GrantedAuthority&gt; getAuthorities() { return authorities; } public void setAuthorities( Set&lt;GrantedAuthority&gt; authorities ) { if ( this.authorities == null ) { this.authorities = authorities; } } // setters, getters } </code></pre> <p>The last thing is to configure spring security to UserLoginService as authentication provider.</p> <p>Example:</p> <pre><code>&lt;security:authentication-manager alias="authenticationManager"&gt; &lt;security:authentication-provider user-service-ref="userLoginService"&gt; &lt;security:password-encoder ref="userPasswordEncoder"/&gt; &lt;/security:authentication-provider&gt; &lt;/security:authentication-manager&gt; &lt;bean &lt;bean id="userLoginService" class="com.stackoverflow.UserLoginService" /&gt; </code></pre> <p>In the jsp page you need to add:</p> <pre><code>&lt;%@ taglib prefix="a" uri="http://www.springframework.org/security/tags"%&gt; &lt;a:authentication property="principal" var="principal" /&gt; </code></pre> <p>Now you can access all the things in UserLoginBean.</p> <pre><code>&lt;c:if test="${not empty principal &amp;&amp; principal != 'anonymousUser'}"&gt; &lt;c:set var="fullname"&gt;&lt;c:out value="${principal.fullname}" /&gt;&lt;/c:set&gt; &lt;/c:if&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