Commit 996c0e67587660bf65d9a6c955e31fa641a44768
1 parent
4364e2fb
1.
Showing
2 changed files
with
60 additions
and
58 deletions
src/main/java/com/bsth/entity/sys/CompanyAuthority.java
| 1 | package com.bsth.entity.sys; | 1 | package com.bsth.entity.sys; |
| 2 | 2 | ||
| 3 | import javax.persistence.*; | 3 | import javax.persistence.*; |
| 4 | +import java.io.Serializable; | ||
| 4 | 5 | ||
| 5 | /** | 6 | /** |
| 6 | * Created by panzhao on 2016/11/22. | 7 | * Created by panzhao on 2016/11/22. |
| 7 | */ | 8 | */ |
| 8 | @Entity | 9 | @Entity |
| 9 | @Table(name = "bsth_c_sys_company_auth") | 10 | @Table(name = "bsth_c_sys_company_auth") |
| 10 | -public class CompanyAuthority { | 11 | +public class CompanyAuthority implements Serializable { |
| 11 | 12 | ||
| 12 | @Id | 13 | @Id |
| 13 | @GeneratedValue(strategy = GenerationType.IDENTITY) | 14 | @GeneratedValue(strategy = GenerationType.IDENTITY) |
src/main/java/com/bsth/security/util/SecurityUtils.java
| 1 | -package com.bsth.security.util; | ||
| 2 | - | ||
| 3 | -import com.bsth.entity.sys.Role; | ||
| 4 | -import com.bsth.entity.sys.SecurityUser; | ||
| 5 | -import com.bsth.entity.sys.SysUser; | ||
| 6 | -import org.slf4j.Logger; | ||
| 7 | -import org.slf4j.LoggerFactory; | ||
| 8 | -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
| 9 | -import org.springframework.security.core.GrantedAuthority; | ||
| 10 | -import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
| 11 | -import org.springframework.security.core.context.SecurityContext; | ||
| 12 | -import org.springframework.security.core.context.SecurityContextHolder; | ||
| 13 | - | ||
| 14 | -import javax.servlet.http.HttpServletRequest; | ||
| 15 | -import java.util.ArrayList; | ||
| 16 | -import java.util.List; | ||
| 17 | -import java.util.Set; | ||
| 18 | - | ||
| 19 | -/** | ||
| 20 | - * @author PanZhao | ||
| 21 | - * @ClassName: SecurityUtils | ||
| 22 | - * @date 2016年3月30日 上午11:28:24 | ||
| 23 | - */ | ||
| 24 | -public class SecurityUtils { | ||
| 25 | - | ||
| 26 | - static Logger logger = LoggerFactory.getLogger(SecurityUtils.class); | ||
| 27 | - | ||
| 28 | - /** | ||
| 29 | - * @return SysUser 返回类型 | ||
| 30 | - * @throws | ||
| 31 | - * @Title: getCurrentUser | ||
| 32 | - * @Description: TODO(获取当前用户) | ||
| 33 | - */ | ||
| 34 | - public static SysUser getCurrentUser() { | ||
| 35 | - SysUser user = null; | ||
| 36 | - try { | ||
| 37 | - user = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | ||
| 38 | - } catch (Exception e) { | ||
| 39 | - logger.error("", e); | ||
| 40 | - } | ||
| 41 | - return user; | ||
| 42 | - } | ||
| 43 | - | ||
| 44 | - public static void login(SysUser user, HttpServletRequest request) { | ||
| 45 | - SecurityUser securityUser = new SecurityUser(user); | ||
| 46 | - SecurityContext sContext = SecurityContextHolder.getContext(); | ||
| 47 | - | ||
| 48 | - List<GrantedAuthority> grantedAuths = new ArrayList<>(); | ||
| 49 | - Set<Role> set = user.getRoles(); | ||
| 50 | - for(Role r : set){ | ||
| 51 | - grantedAuths.add(new SimpleGrantedAuthority(r.getCodeName())); | ||
| 52 | - } | ||
| 53 | - | ||
| 54 | - sContext.setAuthentication(new UsernamePasswordAuthenticationToken(securityUser, securityUser.getAuthorities(), grantedAuths)); | ||
| 55 | - request.getSession(true).setAttribute("SPRING_SECURITY_CONTEXT", sContext); | ||
| 56 | - } | ||
| 57 | -} | 1 | +package com.bsth.security.util; |
| 2 | + | ||
| 3 | +import com.bsth.entity.sys.Role; | ||
| 4 | +import com.bsth.entity.sys.SecurityUser; | ||
| 5 | +import com.bsth.entity.sys.SysUser; | ||
| 6 | +import org.slf4j.Logger; | ||
| 7 | +import org.slf4j.LoggerFactory; | ||
| 8 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
| 9 | +import org.springframework.security.core.Authentication; | ||
| 10 | +import org.springframework.security.core.GrantedAuthority; | ||
| 11 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
| 12 | +import org.springframework.security.core.context.SecurityContext; | ||
| 13 | +import org.springframework.security.core.context.SecurityContextHolder; | ||
| 14 | + | ||
| 15 | +import javax.servlet.http.HttpServletRequest; | ||
| 16 | +import java.util.ArrayList; | ||
| 17 | +import java.util.List; | ||
| 18 | +import java.util.Set; | ||
| 19 | + | ||
| 20 | +/** | ||
| 21 | + * @author PanZhao | ||
| 22 | + * @ClassName: SecurityUtils | ||
| 23 | + * @date 2016年3月30日 上午11:28:24 | ||
| 24 | + */ | ||
| 25 | +public class SecurityUtils { | ||
| 26 | + | ||
| 27 | + static Logger logger = LoggerFactory.getLogger(SecurityUtils.class); | ||
| 28 | + | ||
| 29 | + /** | ||
| 30 | + * @return SysUser 返回类型 | ||
| 31 | + * @throws | ||
| 32 | + * @Title: getCurrentUser | ||
| 33 | + * @Description: TODO(获取当前用户) | ||
| 34 | + */ | ||
| 35 | + public static SysUser getCurrentUser(HttpServletRequest request) { | ||
| 36 | + SysUser user = null; | ||
| 37 | + try { | ||
| 38 | + user = (SysUser)(((SecurityContext)request.getSession().getAttribute("SPRING_SECURITY_CONTEXT")).getAuthentication().getPrincipal()); | ||
| 39 | + } catch (Exception e) { | ||
| 40 | + logger.error("", e); | ||
| 41 | + } | ||
| 42 | + return user; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + public static void login(SysUser user, HttpServletRequest request) { | ||
| 46 | + SecurityUser securityUser = new SecurityUser(user); | ||
| 47 | + SecurityContext sContext = SecurityContextHolder.getContext(); | ||
| 48 | + | ||
| 49 | + List<GrantedAuthority> grantedAuths = new ArrayList<>(); | ||
| 50 | + Set<Role> set = user.getRoles(); | ||
| 51 | + for(Role r : set){ | ||
| 52 | + grantedAuths.add(new SimpleGrantedAuthority(r.getCodeName())); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + sContext.setAuthentication(new UsernamePasswordAuthenticationToken(securityUser, securityUser.getAuthorities(), grantedAuths)); | ||
| 56 | + request.getSession(true).setAttribute("SPRING_SECURITY_CONTEXT", sContext); | ||
| 57 | + } | ||
| 58 | +} |