File tree Expand file tree Collapse file tree
springboot-springSecurity2/src/main/java/com/us/example Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package com .us .example .controller ;
22
3- import com .us .example .dao .UserDao ;
4- import org .springframework .beans .factory .annotation .Autowired ;
5- import org .springframework .security .core .context .SecurityContext ;
6- import org .springframework .security .core .userdetails .User ;
3+ import com .us .example .domain .SysUser ;
4+ import org .springframework .security .core .annotation .AuthenticationPrincipal ;
75import org .springframework .web .bind .annotation .RequestMapping ;
86import org .springframework .web .bind .annotation .RequestParam ;
97import org .springframework .web .bind .annotation .ResponseBody ;
108import org .springframework .web .bind .annotation .RestController ;
119
12- import javax .servlet .http .HttpServletRequest ;
1310
1411/**
1512 * Created by yangyibo on 17/3/1.
1613 */
1714@ RestController
1815public class LoginController {
19- @ Autowired
20- UserDao userDao ;
2116
2217 @ RequestMapping (value = "/login" )
2318 @ ResponseBody
2419 //用户名密码是用base64 加密 原文为 admin:admin 即 用户名:密码 内容是放在request.getHeader 的 "authorization" 中
25- public Object login (HttpServletRequest request , @ RequestParam (name = "logout" , required = false ) String logout ) {
20+ public Object login (@ AuthenticationPrincipal SysUser loginedUser , @ RequestParam (name = "logout" , required = false ) String logout ) {
2621 if (logout != null ) {
2722 return null ;
2823 }
29- SecurityContext sc = (SecurityContext ) request .getSession ().getAttribute ("SPRING_SECURITY_CONTEXT" );
30- User user = (User ) sc .getAuthentication ().getPrincipal ();
31- return userDao .findByUserName (user .getUsername ());
24+ if (loginedUser != null ) {
25+ return loginedUser ;
26+ }
27+ return null ;
3228 }
3329}
Original file line number Diff line number Diff line change 11package com .us .example .domain ;
22
3+ import com .fasterxml .jackson .annotation .JsonIgnore ;
4+ import org .springframework .security .core .GrantedAuthority ;
5+ import org .springframework .security .core .userdetails .UserDetails ;
6+
7+ import java .util .Collection ;
38import java .util .List ;
49
510/**
611 * Created by yangyibo on 17/1/17.
712 */
813
9- public class SysUser {
14+ public class SysUser implements UserDetails { // implements UserDetails 用于登录时 @AuthenticationPrincipal 标签取值
1015 private Integer id ;
1116 private String username ;
17+ @ JsonIgnore
1218 private String password ;
13-
1419 private List <SysRole > roles ;
20+ private List <? extends GrantedAuthority > authorities ;
21+
1522
1623 public Integer getId () {
1724 return id ;
@@ -45,4 +52,35 @@ public void setRoles(List<SysRole> roles) {
4552 this .roles = roles ;
4653 }
4754
55+ @ JsonIgnore
56+ @ Override
57+ public boolean isAccountNonExpired () {
58+ return true ;
59+ }
60+ @ JsonIgnore
61+ @ Override
62+ public boolean isAccountNonLocked () {
63+ return true ;
64+ }
65+ @ JsonIgnore
66+ @ Override
67+ public boolean isCredentialsNonExpired () {
68+ return true ;
69+ }
70+
71+
72+ @ JsonIgnore
73+ @ Override
74+ public boolean isEnabled () {
75+ return true ;
76+ }
77+ @ JsonIgnore
78+ @ Override
79+ public Collection <? extends GrantedAuthority > getAuthorities () {
80+ return authorities ;
81+ }
82+ public void setGrantedAuthorities (List <? extends GrantedAuthority > authorities ) {
83+ this .authorities = authorities ;
84+ }
85+
4886}
Original file line number Diff line number Diff line change 33import com .us .example .dao .UserDao ;
44import com .us .example .domain .SysRole ;
55import com .us .example .domain .SysUser ;
6+ import org .slf4j .LoggerFactory ;
67import org .springframework .beans .factory .annotation .Autowired ;
78import org .springframework .security .core .authority .SimpleGrantedAuthority ;
89import org .springframework .security .core .userdetails .UserDetails ;
@@ -21,6 +22,7 @@ public class CustomUserService implements UserDetailsService { //自定义UserDe
2122
2223 @ Autowired
2324 UserDao userDao ;
25+ private static final org .slf4j .Logger logger = LoggerFactory .getLogger (CustomUserService .class );
2426
2527 @ Override
2628 public UserDetails loadUserByUsername (String username ) { //重写loadUserByUsername 方法获得 userdetails 类型用户
@@ -34,11 +36,10 @@ public UserDetails loadUserByUsername(String username) { //重写loadUserByUsern
3436 for (SysRole role :user .getRoles ())
3537 {
3638 authorities .add (new SimpleGrantedAuthority (role .getName ()));
37- System . out . println ( role . getName () );
39+ logger . info ( "loadUserByUsername: " + user );
3840 }
39- return new org .springframework .security .core .userdetails .User (user .getUsername (),
40- user .getPassword (), authorities );
41-
41+ user .setGrantedAuthorities (authorities ); //用于登录时 @AuthenticationPrincipal 标签取值
42+ return user ;
4243 }
4344
4445}
You can’t perform that action at this time.
0 commit comments