Spring Security 取Session中的值和修改userDetails(转)

1.在session中取得spring security的登录用户名如下

${session.SPRING_SECURITY_CONTEXT.authentication.principal.username}

spring security 把SPRING_SECURITY_CONTEXT 放入了session 没有直接把username 放进去。

下面一段代码主要描述的是session中的存的变量

view plaincopy to clipboardprint?
存跳转时候的URL  
session
{SPRING_SECURITY_SAVED_REQUEST_KEY=SavedRequest[http://localhost:8080/AVerPortal/resourceAction/resourceIndex.action]}
 
 
存的是登录成功时候session中存的信息  
session
{SPRING_SECURITY_CONTEXT=org.springframework.security.context.SecurityContextImpl@87b16984:
Authentication:
org.springframework.security.providers.cas.CasAuthenticationToken@87b16984:
Principal: com.avi.casExtends.UserInfo@ff631d80: Username: test; Password:
[PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired:
true; AccountNonLocked: true; Granted Authorities: ROLE_ADMIN; Password:
[PROTECTED]; Authenticated: true; Details:
org.springframework.security.ui.WebAuthenticationDetails@12afc: RemoteIpAddress:
127.0.0.1; SessionId: AE56E8925195DFF4C50ABD384574CCEA; Granted Authorities:
ROLE_ADMIN Assertion: org.jasig.cas.client.validation.AssertionImpl@661a11
Credentials (Service/Proxy Ticket): ST-3-1lX3acgZ6HNgmhvjXuxB-cas, userId=2,
userName=test} 
存跳转时候的URL
session
{SPRING_SECURITY_SAVED_REQUEST_KEY=SavedRequest[http://localhost:8080/AVerPortal/resourceAction/resourceIndex.action]}


存的是登录成功时候session中存的信息
session
{SPRING_SECURITY_CONTEXT=org.springframework.security.context.SecurityContextImpl@87b16984:
Authentication:
org.springframework.security.providers.cas.CasAuthenticationToken@87b16984:
Principal: com.avi.casExtends.UserInfo@ff631d80: Username: test; Password:
[PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired:
true; AccountNonLocked: true; Granted Authorities: ROLE_ADMIN; Password:
[PROTECTED]; Authenticated: true; Details:
org.springframework.security.ui.WebAuthenticationDetails@12afc: RemoteIpAddress:
127.0.0.1; SessionId: AE56E8925195DFF4C50ABD384574CCEA; Granted Authorities:
ROLE_ADMIN Assertion: org.jasig.cas.client.validation.AssertionImpl@661a11
Credentials (Service/Proxy Ticket): ST-3-1lX3acgZ6HNgmhvjXuxB-cas, userId=2,
userName=test}

2.在页面端用tag获取

Java代码 
  <%@ taglib prefix='security'
  uri='http://www.springframework.org/security/tags'%>   
   
  <security:authentication
  property="principal.username"></security:authentication>  

<%@ taglib prefix='security' uri='http://www.springframework.org/security/tags'%>

<security:authentication property="principal.username"></security:authentication>
或者


Java代码 
  <security:authorize ifAllGranted="ROLE_ADMIN">  
   
   <security:authentication
  property="principal.username"></security:authentication>   
   
  </security:authorize> 

<security:authorize ifAllGranted="ROLE_ADMIN">

 <security:authentication property="principal.username"></security:authentication>

</security:authorize>

或者取session中的值

#session.SPRING_SECURITY_CONTEXT.authentication.principal.username等同于

3.在后台获取

Java代码 
  UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()  
      .getAuthentication()  
      .getPrincipal();  
   
   
  userDetails.getUsername() 

UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
    .getAuthentication()
    .getPrincipal();


userDetails.getUsername()

想要获取更多的信息得扩展userDetails的默认实现类user类和UserDetailsService接口

由于springsecurity是把整个user信息放入session中的即:session.SPRING_SECURITY_CONTEXT.authentication.principal


这个就是代表着user对象

因此我做了扩展增加user里的信息 加上userId

代码如下:扩展user

Java代码 
   expand sourceview plaincopy to clipboardprint?  
  package com.avi.casExtends;     
      
  import org.springframework.security.GrantedAuthority;     
  import org.springframework.security.userdetails.User;     
      
  public class UserInfo extends User{     
      private static final long serialVersionUID = 1L;     
      
      private String userId;     
      
      @SuppressWarnings("deprecation")     
      public UserInfo(String username, String password, boolean enabled,
  GrantedAuthority[] authorities)     
          throws IllegalArgumentException {     
          super(username,password, enabled, authorities);     
      }     
      
      public String getUserId() {     
          return userId;     
      }     
      
      public void setUserId(String userId) {     
          this.userId = userId;     
      }     
      
      public static long getSerialVersionUID() {     
          return serialVersionUID;     
      }     
      
         
  }    
  package com.avi.casExtends;  
   
  import org.springframework.security.GrantedAuthority;  
  import org.springframework.security.userdetails.User;  
   
  public class UserInfo extends User{  
   private static final long serialVersionUID = 1L;  
   
      private String userId;  
   
      @SuppressWarnings("deprecation")  
   public UserInfo(String username, String password, boolean enabled,
  GrantedAuthority[] authorities)  
          throws IllegalArgumentException {  
          super(username,password, enabled, authorities);  
      }  
   
   public String getUserId() {  
    return userId;  
   }  
   
   public void setUserId(String userId) {  
    this.userId = userId;  
   }  
   
   public static long getSerialVersionUID() {  
    return serialVersionUID;  
   }  
   
      
  }  
     
   
  实现userDetailsservice接口  
   
  + expand sourceview plaincopy to clipboardprint?  
  package com.avi.casExtends;     
      
  import java.util.HashMap;     
  import java.util.List;     
  import java.util.Map;     
      
  import org.springframework.dao.DataAccessException;     
  import org.springframework.security.GrantedAuthority;     
  import org.springframework.security.GrantedAuthorityImpl;     
  import org.springframework.security.userdetails.UserDetails;     
  import org.springframework.security.userdetails.UserDetailsService;     
  import org.springframework.security.userdetails.UsernameNotFoundException;    
  
      
  import com.avi.dao.AccountDao;     
  import com.avi.data.User;     
      
  public class UserInfoService implements UserDetailsService{     
           
      private AccountDao accountDao;     
      private Map<String, UserInfo> userMap = null;     
      
      public UserInfoService() {     
              
               
      }     
      public void fillMap(){     
           userMap = new HashMap<String, UserInfo>();     
           List<User> users = accountDao.findAllUsers();     
           UserInfo userInfo = null;     
           for(User user:users){     
              userInfo = new
  UserInfo(user.getUserName(),user.getPassword(),true,new GrantedAuthority[]{   
   
                  new GrantedAuthorityImpl(user.getRole()),     
              });     
              userInfo.setUserId(user.getId().toString());     
                   
               userMap.put(user.getUserName(), userInfo);     
           }     
      }     
           
      public UserDetails loadUserByUsername(String username)     
          throws UsernameNotFoundException, DataAccessException {     
          if(userMap==null)     
              fillMap();     
          return userMap.get(username);     
      }     
      
      public AccountDao getAccountDao() {     
          return accountDao;     
      }     
      
      public void setAccountDao(AccountDao accountDao) {     
          this.accountDao = accountDao;     
      }     
      
      public Map<String, UserInfo> getUserMap() {     
          return userMap;     
      }     
      
      public void setUserMap(Map<String, UserInfo> userMap) {     
          this.userMap = userMap;     
      }     
      
  }    
  package com.avi.casExtends;  
   
  import java.util.HashMap;  
  import java.util.List;  
  import java.util.Map;  
   
  import org.springframework.dao.DataAccessException;  
  import org.springframework.security.GrantedAuthority;  
  import org.springframework.security.GrantedAuthorityImpl;  
  import org.springframework.security.userdetails.UserDetails;  
  import org.springframework.security.userdetails.UserDetailsService;  
  import org.springframework.security.userdetails.UsernameNotFoundException;  
   
  import com.avi.dao.AccountDao;  
  import com.avi.data.User;  
   
  public class UserInfoService implements UserDetailsService{  
     
   private AccountDao accountDao;  
   private Map<String, UserInfo> userMap = null;  
   
      public UserInfoService() {  
           
            
      }  
      public void fillMap(){  
        userMap = new HashMap<String, UserInfo>();  
           List<User> users = accountDao.findAllUsers();  
           UserInfo userInfo = null;  
           for(User user:users){  
            userInfo = new
  UserInfo(user.getUserName(),user.getPassword(),true,new GrantedAuthority[]{  
             new GrantedAuthorityImpl(user.getRole()),  
            });  
            userInfo.setUserId(user.getId().toString());  
              
               userMap.put(user.getUserName(), userInfo);  
           }  
      }  
        
      public UserDetails loadUserByUsername(String username)  
          throws UsernameNotFoundException, DataAccessException {  
       if(userMap==null)  
        fillMap();  
          return userMap.get(username);  
      }  
   
   public AccountDao getAccountDao() {  
    return accountDao;  
   }  
   
   public void setAccountDao(AccountDao accountDao) {  
    this.accountDao = accountDao;  
   }  
   
   public Map<String, UserInfo> getUserMap() {  
    return userMap;  
   }  
   
   public void setUserMap(Map<String, UserInfo> userMap) {  
    this.userMap = userMap;  
   }  
   
  }  
     
   
   private AccountDao accountDao;是注入进来的查数据库的类  
   
  然后修改XML文件指定所要用到的service  
   
  + expand sourceview plaincopy to clipboardprint?  
  <authentication-provider user-service-ref="userDetailsService"/>     
      
  <bean id="userDetailsService" class="com.avi.casExtends.UserInfoService"
  singleton="false">     
          <property name="accountDao" ref="accountDao"/>     
  </bean>    
  <authentication-provider user-service-ref="userDetailsService"/>  
   
  <bean id="userDetailsService" class="com.avi.casExtends.UserInfoService"
  singleton="false">  
    <property name="accountDao" ref="accountDao"/>  
  </bean>   
   
   
  ${session.SPRING_SECURITY_CONTEXT.authentication.principal.username} 

 expand sourceview plaincopy to clipboardprint?
package com.avi.casExtends;  
 
import org.springframework.security.GrantedAuthority;  
import org.springframework.security.userdetails.User;  
 
public class UserInfo extends User{  
    private static final long serialVersionUID = 1L;  
 
    private String userId;  
 
    @SuppressWarnings("deprecation")  
    public UserInfo(String username, String password, boolean enabled, GrantedAuthority[] authorities)  
        throws IllegalArgumentException {  
        super(username,password, enabled, authorities);  
    }  
 
    public String getUserId() {  
        return userId;  
    }  
 
    public void setUserId(String userId) {  
        this.userId = userId;  
    }  
 
    public static long getSerialVersionUID() {  
        return serialVersionUID;  
    }  
 
    

package com.avi.casExtends;

import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.User;

public class UserInfo extends User{
 private static final long serialVersionUID = 1L;

    private String userId;

    @SuppressWarnings("deprecation")
 public UserInfo(String username, String password, boolean enabled, GrantedAuthority[] authorities)
        throws IllegalArgumentException {
        super(username,password, enabled, authorities);
    }

 public String getUserId() {
  return userId;
 }

 public void setUserId(String userId) {
  this.userId = userId;
 }

 public static long getSerialVersionUID() {
  return serialVersionUID;
 }

 
}
 

实现userDetailsservice接口

+ expand sourceview plaincopy to clipboardprint?
package com.avi.casExtends;  
 
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
 
import org.springframework.dao.DataAccessException;  
import org.springframework.security.GrantedAuthority;  
import org.springframework.security.GrantedAuthorityImpl;  
import org.springframework.security.userdetails.UserDetails;  
import org.springframework.security.userdetails.UserDetailsService;  
import org.springframework.security.userdetails.UsernameNotFoundException;  
 
import com.avi.dao.AccountDao;  
import com.avi.data.User;  
 
public class UserInfoService implements UserDetailsService{  
      
    private AccountDao accountDao;  
    private Map<String, UserInfo> userMap = null;  
 
    public UserInfoService() {  
         
          
    }  
    public void fillMap(){  
         userMap = new HashMap<String, UserInfo>();  
         List<User> users = accountDao.findAllUsers();  
         UserInfo userInfo = null;  
         for(User user:users){  
            userInfo = new UserInfo(user.getUserName(),user.getPassword(),true,new GrantedAuthority[]{  
                new GrantedAuthorityImpl(user.getRole()),  
            });  
            userInfo.setUserId(user.getId().toString());  
              
             userMap.put(user.getUserName(), userInfo);  
         }  
    }  
      
    public UserDetails loadUserByUsername(String username)  
        throws UsernameNotFoundException, DataAccessException {  
        if(userMap==null)  
            fillMap();  
        return userMap.get(username);  
    }  
 
    public AccountDao getAccountDao() {  
        return accountDao;  
    }  
 
    public void setAccountDao(AccountDao accountDao) {  
        this.accountDao = accountDao;  
    }  
 
    public Map<String, UserInfo> getUserMap() {  
        return userMap;  
    }  
 
    public void setUserMap(Map<String, UserInfo> userMap) {  
        this.userMap = userMap;  
    }  
 

package com.avi.casExtends;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;

import com.avi.dao.AccountDao;
import com.avi.data.User;

public class UserInfoService implements UserDetailsService{
 
 private AccountDao accountDao;
 private Map<String, UserInfo> userMap = null;

    public UserInfoService() {
      
       
    }
    public void fillMap(){
      userMap = new HashMap<String, UserInfo>();
         List<User> users = accountDao.findAllUsers();
         UserInfo userInfo = null;
         for(User user:users){
          userInfo = new UserInfo(user.getUserName(),user.getPassword(),true,new GrantedAuthority[]{
           new GrantedAuthorityImpl(user.getRole()),
          });
          userInfo.setUserId(user.getId().toString());
         
             userMap.put(user.getUserName(), userInfo);
         }
    }
   
    public UserDetails loadUserByUsername(String username)
        throws UsernameNotFoundException, DataAccessException {
     if(userMap==null)
      fillMap();
        return userMap.get(username);
    }

 public AccountDao getAccountDao() {
  return accountDao;
 }

 public void setAccountDao(AccountDao accountDao) {
  this.accountDao = accountDao;
 }

 public Map<String, UserInfo> getUserMap() {
  return userMap;
 }

 public void setUserMap(Map<String, UserInfo> userMap) {
  this.userMap = userMap;
 }

}
 

 private AccountDao accountDao;是注入进来的查数据库的类

然后修改XML文件指定所要用到的service

+ expand sourceview plaincopy to clipboardprint?
<authentication-provider user-service-ref="userDetailsService"/>  
 
<bean id="userDetailsService" class="com.avi.casExtends.UserInfoService" singleton="false">  
        <property name="accountDao" ref="accountDao"/>  
</bean> 
<authentication-provider user-service-ref="userDetailsService"/>

<bean id="userDetailsService" class="com.avi.casExtends.UserInfoService" singleton="false">
  <property name="accountDao" ref="accountDao"/>
</bean>


${session.SPRING_SECURITY_CONTEXT.authentication.principal.username}

好的,这是一个比较常见的需求,下面是大致的步骤: 1. 在pom.xml引入spring-security和jwt相关依赖 ```xml <!-- Spring Security --> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>${spring-security.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>${spring-security.version}</version> </dependency> <!-- JWT --> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>${jjwt.version}</version> </dependency> ``` 其 `${spring-security.version}` 和 `${jjwt.version}` 分别为 Spring Security 和 JWT 的版本号。 2. 配置 Spring SecuritySpring Boot ,使用 Java Config 来配置 Spring Security。具体的配置可以参考如下代码: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Autowired private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint; @Bean public JwtAuthenticationFilter jwtAuthenticationFilter() { return new JwtAuthenticationFilter(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable() .authorizeRequests() .antMatchers("/api/auth/**") .permitAll() .anyRequest() .authenticated() .and() .exceptionHandling() .authenticationEntryPoint(jwtAuthenticationEntryPoint) .and() .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS); http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } ``` 上面的代码,我们配置了: - `userDetailsService`:用于从数据库用户信息。 - `JwtAuthenticationEntryPoint`:用于处理认证失败的情况。 - `JwtAuthenticationFilter`:用于处理 JWT 认证。 在 `configure(HttpSecurity http)` ,我们配置了哪些请求需要认证,哪些请求不需要认证,以及异常处理和 session 管理等。 3. 配置 JWT 在 JWT ,我们需要定义一个 secret key 用于签名和验证 JWT。可以在 application.properties 配置: ```properties jwt.secret=mySecretKey jwt.expirationMs=86400000 ``` 其,`jwt.secret` 是用于签名和验证 JWT 的 secret key,`jwt.expirationMs` 是 JWT 的过期时间(单位为毫秒)。 然后,我们可以定义一个 `JwtUtils` 类来生成和解析 JWT: ```java @Component public class JwtUtils { @Value("${jwt.secret}") private String jwtSecret; @Value("${jwt.expirationMs}") private int jwtExpirationMs; public String generateJwtToken(Authentication authentication) { UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal(); return Jwts.builder() .setSubject(userPrincipal.getUsername()) .setIssuedAt(new Date()) .setExpiration(new Date((new Date()).getTime() + jwtExpirationMs)) .signWith(SignatureAlgorithm.HS512, jwtSecret) .compact(); } public String getUsernameFromJwtToken(String token) { return Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token).getBody().getSubject(); } public boolean validateJwtToken(String authToken) { try { Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(authToken); return true; } catch (SignatureException e) { logger.error("Invalid JWT signature: {}", e.getMessage()); } catch (MalformedJwtException e) { logger.error("Invalid JWT token: {}", e.getMessage()); } catch (ExpiredJwtException e) { logger.error("JWT token is expired: {}", e.getMessage()); } catch (UnsupportedJwtException e) { logger.error("JWT token is unsupported: {}", e.getMessage()); } catch (IllegalArgumentException e) { logger.error("JWT claims string is empty: {}", e.getMessage()); } return false; } } ``` 上面的代码,我们使用了 `Jwts.builder()` 和 `Jwts.parser()` 来生成和解析 JWT。 4. 配置认证接口 最后,我们可以在认证接口生成 JWT 并返回给客户端: ```java @RestController @RequestMapping("/api/auth") public class AuthController { @Autowired private AuthenticationManager authenticationManager; @Autowired private JwtUtils jwtUtils; @Autowired private UserDetailsService userDetailsService; @PostMapping("/signin") public ResponseEntity<?> authenticateUser(@RequestBody LoginRequest loginRequest) { Authentication authentication = authenticationManager.authenticate( new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword())); SecurityContextHolder.getContext().setAuthentication(authentication); String jwt = jwtUtils.generateJwtToken(authentication); UserDetails userDetails = userDetailsService.loadUserByUsername(loginRequest.getUsername()); return ResponseEntity.ok(new JwtResponse(jwt, userDetails.getUsername(), userDetails.getAuthorities())); } } ``` 上面的代码,我们使用了 `AuthenticationManager` 来进行认证,然后使用 `JwtUtils` 生成 JWT 并返回给客户端。 以上就是整合 Spring Security 和 JWT 的大致步骤,具体实现过程还需根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值