Skip to content

Commit 4cbfd1d

Browse files
committed
Migrate from Acegi to Spring Security
1 parent 1c2b389 commit 4cbfd1d

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

src/main/java/hudson/security/PAMSecurityRealm.java

+16-16
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,23 @@
3737
import java.nio.file.attribute.PosixFilePermission;
3838
import jenkins.model.IdStrategy;
3939
import jenkins.model.Jenkins;
40-
import org.acegisecurity.AuthenticationException;
41-
import org.acegisecurity.BadCredentialsException;
42-
import org.acegisecurity.GrantedAuthority;
43-
import org.acegisecurity.GrantedAuthorityImpl;
44-
import org.acegisecurity.userdetails.User;
45-
import org.acegisecurity.userdetails.UserDetails;
46-
import org.acegisecurity.userdetails.UsernameNotFoundException;
4740

4841
import org.jvnet.libpam.PAM;
4942
import org.jvnet.libpam.PAMException;
5043
import org.jvnet.libpam.UnixUser;
5144
import org.kohsuke.stapler.DataBoundConstructor;
5245
import org.kohsuke.stapler.interceptor.RequirePOST;
53-
import org.springframework.dao.DataAccessException;
46+
import org.springframework.security.authentication.BadCredentialsException;
47+
import org.springframework.security.core.AuthenticationException;
48+
import org.springframework.security.core.GrantedAuthority;
49+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
50+
import org.springframework.security.core.userdetails.User;
51+
import org.springframework.security.core.userdetails.UserDetails;
52+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
5453

5554
import java.io.File;
55+
import java.util.ArrayList;
56+
import java.util.Collection;
5657
import java.util.Set;
5758
import java.util.logging.Logger;
5859

@@ -78,7 +79,7 @@ public PAMSecurityRealm(String serviceName) {
7879
}
7980

8081
@Override
81-
protected synchronized UserDetails authenticate(String username, String password) throws AuthenticationException {
82+
protected synchronized UserDetails authenticate2(String username, String password) throws AuthenticationException {
8283
try {
8384
UnixUser u = new PAM(serviceName).authenticate(username, password);
8485

@@ -90,7 +91,7 @@ protected synchronized UserDetails authenticate(String username, String password
9091
}
9192

9293
@Override
93-
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
94+
public UserDetails loadUserByUsername2(String username) throws UsernameNotFoundException {
9495
if (!UnixUser.exists(username)) {
9596
throw new UsernameNotFoundException("No such Unix user: " + username);
9697
}
@@ -103,19 +104,18 @@ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundEx
103104
}
104105
}
105106

106-
private static GrantedAuthority[] toAuthorities(UnixUser u) {
107+
private static Collection<? extends GrantedAuthority> toAuthorities(UnixUser u) {
107108
Set<String> groups = u.getGroups();
108-
GrantedAuthority[] authorities = new GrantedAuthority[groups.size() + 1];
109-
int i = 0;
109+
Collection<GrantedAuthority> authorities = new ArrayList<>();
110110
for (String group : groups) {
111-
authorities[i++] = new GrantedAuthorityImpl(group);
111+
authorities.add(new SimpleGrantedAuthority(group));
112112
}
113-
authorities[i] = AUTHENTICATED_AUTHORITY;
113+
authorities.add(AUTHENTICATED_AUTHORITY2);
114114
return authorities;
115115
}
116116

117117
@Override
118-
public GroupDetails loadGroupByGroupname(String groupName) throws UsernameNotFoundException, DataAccessException {
118+
public GroupDetails loadGroupByGroupname2(String groupName, boolean fetchMembers) throws UsernameNotFoundException {
119119
String group = groupName.startsWith("@") ? groupName.substring(1) : groupName;
120120

121121
try {

src/test/java/hudson/security/PAMSecurityRealmTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
import hudson.Functions;
77
import hudson.security.SecurityRealm.SecurityComponents;
8-
import org.acegisecurity.userdetails.UsernameNotFoundException;
98
import org.junit.Rule;
109
import org.junit.Test;
1110
import org.jvnet.hudson.test.JenkinsRule;
11+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
1212

1313
import java.util.Arrays;
1414

@@ -27,14 +27,14 @@ public void loadUsers() {
2727
SecurityComponents sc = new PAMSecurityRealm("sshd").getSecurityComponents();
2828

2929
try {
30-
sc.userDetails.loadUserByUsername("bogus-bogus-bogus");
30+
sc.userDetails2.loadUserByUsername("bogus-bogus-bogus");
3131
fail("no such user");
3232
} catch (UsernameNotFoundException e) {
3333
// expected
3434
}
3535

3636

3737
String name = System.getProperty("user.name");
38-
System.out.println(Arrays.asList(sc.userDetails.loadUserByUsername(name).getAuthorities()));
38+
System.out.println(Arrays.asList(sc.userDetails2.loadUserByUsername(name).getAuthorities()));
3939
}
4040
}

0 commit comments

Comments
 (0)