Revision 60129
Added by Konstantinos Triantafyllou almost 3 years ago
modules/uoa-authorization-library/branches/redis/src/main/java/eu/dnetlib/uoaauthorizationlibrary/utils/AuthorizationUtils.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.utils; |
|
2 |
|
|
3 |
import com.google.gson.Gson; |
|
4 |
import eu.dnetlib.uoaauthorizationlibrary.configuration.SecurityConfig; |
|
5 |
import org.apache.log4j.Logger; |
|
6 |
import org.springframework.beans.factory.annotation.Autowired; |
|
7 |
import org.springframework.stereotype.Component; |
|
8 |
|
|
9 |
import javax.servlet.http.Cookie; |
|
10 |
import javax.servlet.http.HttpServletRequest; |
|
11 |
import java.io.BufferedReader; |
|
12 |
import java.io.InputStreamReader; |
|
13 |
import java.io.StringReader; |
|
14 |
import java.net.HttpURLConnection; |
|
15 |
import java.net.URL; |
|
16 |
|
|
17 |
@Component |
|
18 |
public class AuthorizationUtils { |
|
19 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
20 |
private SecurityConfig securityConfig; |
|
21 |
|
|
22 |
@Autowired |
|
23 |
AuthorizationUtils(SecurityConfig securityConfig) { |
|
24 |
this.securityConfig = securityConfig; |
|
25 |
} |
|
26 |
|
|
27 |
public String getToken(HttpServletRequest request) { |
|
28 |
return this.getCookie(request, "AccessToken"); |
|
29 |
} |
|
30 |
|
|
31 |
public boolean checkCookies(HttpServletRequest request) { |
|
32 |
boolean valid = true; |
|
33 |
String cookieValue = this.getCookie(request, "AccessToken"); |
|
34 |
if (cookieValue == null || cookieValue.isEmpty()) { |
|
35 |
log.info("no cookie available "); |
|
36 |
valid = false; |
|
37 |
} |
|
38 |
return valid; |
|
39 |
} |
|
40 |
|
|
41 |
private String getCookie(HttpServletRequest request, String cookieName) { |
|
42 |
if (request.getCookies() == null) { |
|
43 |
return null; |
|
44 |
} |
|
45 |
for (Cookie c : request.getCookies()) { |
|
46 |
if (c.getName().equals(cookieName)) { |
|
47 |
return c.getValue(); |
|
48 |
} |
|
49 |
|
|
50 |
} |
|
51 |
return null; |
|
52 |
} |
|
53 |
|
|
54 |
public UserInfo getUserInfo(String accessToken) { |
|
55 |
String url = securityConfig.getUserInfoUrl() + accessToken; |
|
56 |
try { |
|
57 |
URL obj = new URL(url); |
|
58 |
HttpURLConnection con = (HttpURLConnection) obj.openConnection(); |
|
59 |
if (con.getResponseCode() != 200) { |
|
60 |
log.debug("User info response code is: " + con.getResponseCode()); |
|
61 |
return null; |
|
62 |
} |
|
63 |
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); |
|
64 |
StringBuilder response = new StringBuilder(); |
|
65 |
String inputLine; |
|
66 |
while ((inputLine = in.readLine()) != null) { |
|
67 |
response.append(inputLine).append("\n"); |
|
68 |
} |
|
69 |
in.close(); |
|
70 |
return json2UserInfo(response.toString()); |
|
71 |
} catch (Exception e) { |
|
72 |
log.error("An error occured while trying to fetch user info ", e); |
|
73 |
return null; |
|
74 |
} |
|
75 |
} |
|
76 |
|
|
77 |
private UserInfo json2UserInfo(String json) { |
|
78 |
if (json == null) { |
|
79 |
return null; |
|
80 |
} |
|
81 |
BufferedReader br = new BufferedReader(new StringReader(json)); |
|
82 |
//convert the json string back to object |
|
83 |
Gson gson = new Gson(); |
|
84 |
UserInfo userInfo = null; |
|
85 |
try { |
|
86 |
userInfo = gson.fromJson(br, UserInfo.class); |
|
87 |
} catch (Exception e) { |
|
88 |
log.debug("Error in parsing json response. Given json is : " + json, e); |
|
89 |
return null; |
|
90 |
} |
|
91 |
try { |
|
92 |
if (userInfo != null && userInfo.getEdu_person_entitlements() != null) { |
|
93 |
for (int i = 0; i < userInfo.getEdu_person_entitlements().size(); i++) { |
|
94 |
String role = userInfo.getEdu_person_entitlements().get(i); |
|
95 |
role = role.split(":")[role.split(":").length - 1]; |
|
96 |
role = role.replace("+", " "); |
|
97 |
userInfo.getEdu_person_entitlements().set(i, role); |
|
98 |
} |
|
99 |
} |
|
100 |
} catch (Exception e) { |
|
101 |
log.debug("Error in parsing Edu_person_entitlements : ", e); |
|
102 |
return null; |
|
103 |
} |
|
104 |
return userInfo; |
|
105 |
} |
|
106 |
|
|
107 |
public boolean isAuthorized(String token) { |
|
108 |
UserInfo userInfo = getUserInfo(token); |
|
109 |
if (userInfo != null) { |
|
110 |
return true; |
|
111 |
} else { |
|
112 |
log.debug(" User has no Valid UserInfo"); |
|
113 |
return false; |
|
114 |
} |
|
115 |
|
|
116 |
} |
|
117 |
} |
modules/uoa-authorization-library/branches/redis/src/main/java/eu/dnetlib/uoaauthorizationlibrary/utils/UserInfo.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.utils; |
|
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.List; |
|
5 |
|
|
6 |
public class UserInfo { |
|
7 |
String sub; |
|
8 |
String name; |
|
9 |
String email; |
|
10 |
List<String> edu_person_entitlements = new ArrayList<String>(); |
|
11 |
List<String> roles = new ArrayList<>(); |
|
12 |
|
|
13 |
@Override |
|
14 |
public String toString() { |
|
15 |
return "UserInfo{" + |
|
16 |
"sub='" + sub + '\'' + |
|
17 |
"name='" + name + '\'' + |
|
18 |
", email='" + email + '\'' + |
|
19 |
", edu_person_entitlements=" + edu_person_entitlements + |
|
20 |
", roles=" + roles + |
|
21 |
'}'; |
|
22 |
} |
|
23 |
|
|
24 |
public String getSub() { |
|
25 |
return sub; |
|
26 |
} |
|
27 |
|
|
28 |
public void setSub(String sub) { |
|
29 |
this.sub = sub; |
|
30 |
} |
|
31 |
|
|
32 |
public String getName() { |
|
33 |
return name; |
|
34 |
} |
|
35 |
|
|
36 |
public void setName(String name) { |
|
37 |
this.name = name; |
|
38 |
} |
|
39 |
|
|
40 |
public String getEmail() { |
|
41 |
return email; |
|
42 |
} |
|
43 |
|
|
44 |
public void setEmail(String email) { |
|
45 |
this.email = email; |
|
46 |
} |
|
47 |
|
|
48 |
public List<String> getEdu_person_entitlements() { |
|
49 |
return edu_person_entitlements; |
|
50 |
} |
|
51 |
|
|
52 |
public void setEdu_person_entitlements(List<String> edu_person_entitlements) { |
|
53 |
this.edu_person_entitlements = edu_person_entitlements; |
|
54 |
} |
|
55 |
|
|
56 |
public List<String> getRoles() { |
|
57 |
return roles; |
|
58 |
} |
|
59 |
|
|
60 |
public void setRoles(List<String> roles) { |
|
61 |
this.roles = roles; |
|
62 |
} |
|
63 |
} |
modules/uoa-authorization-library/branches/redis/src/main/java/eu/dnetlib/uoaauthorizationlibrary/configuration/AuthorizationConfiguration.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.configuration; |
|
2 |
|
|
3 |
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
|
4 |
import org.springframework.context.annotation.*; |
|
5 |
|
|
6 |
@Configuration |
|
7 |
//@PropertySources({ |
|
8 |
// @PropertySource("classpath:authorization.properties"), |
|
9 |
// @PropertySource(value = "classpath:dnet-override.properties", ignoreResourceNotFound = true) |
|
10 |
//}) |
|
11 |
@EnableConfigurationProperties({SecurityConfig.class}) |
|
12 |
@ComponentScan(basePackages = { "eu.dnetlib.uoaauthorizationlibrary" }) |
|
13 |
public class AuthorizationConfiguration { } |
modules/uoa-authorization-library/branches/redis/src/main/java/eu/dnetlib/uoaauthorizationlibrary/configuration/SecurityConfig.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.configuration; |
|
2 |
|
|
3 |
import org.springframework.boot.context.properties.ConfigurationProperties; |
|
4 |
|
|
5 |
@ConfigurationProperties("authorization.security") |
|
6 |
public class SecurityConfig { |
|
7 |
|
|
8 |
private String userInfoUrl; |
|
9 |
private String originServer; |
|
10 |
|
|
11 |
public String getUserInfoUrl() { |
|
12 |
return userInfoUrl; |
|
13 |
} |
|
14 |
|
|
15 |
public void setUserInfoUrl(String userInfoUrl) { |
|
16 |
this.userInfoUrl = userInfoUrl; |
|
17 |
} |
|
18 |
|
|
19 |
public String getOriginServer() { |
|
20 |
return originServer; |
|
21 |
} |
|
22 |
|
|
23 |
public void setOriginServer(String originServer) { |
|
24 |
this.originServer = originServer; |
|
25 |
} |
|
26 |
|
|
27 |
} |
modules/uoa-authorization-library/branches/redis/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/AuthorizationService.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.security; |
|
2 |
|
|
3 |
import org.apache.log4j.Logger; |
|
4 |
import org.springframework.security.core.Authentication; |
|
5 |
import org.springframework.security.core.GrantedAuthority; |
|
6 |
import org.springframework.security.core.context.SecurityContextHolder; |
|
7 |
import org.springframework.security.core.userdetails.User; |
|
8 |
import org.springframework.stereotype.Component; |
|
9 |
|
|
10 |
import java.util.ArrayList; |
|
11 |
import java.util.List; |
|
12 |
|
|
13 |
@Component(value = "AuthorizationService") |
|
14 |
public class AuthorizationService { |
|
15 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
16 |
|
|
17 |
|
|
18 |
public final String SUPER_ADMIN = "SUPER_ADMINISTRATOR"; |
|
19 |
public final String PORTAL_ADMIN = "PORTAL_ADMINISTRATOR"; |
|
20 |
public final String USER_ADMIN = "USER_MANAGER"; |
|
21 |
public final String ANONYMOUS_USER = "ROLE_ANONYMOUS"; |
|
22 |
|
|
23 |
private String mapType(String type) { |
|
24 |
if(type.equals("organization")) { |
|
25 |
type = "institution"; |
|
26 |
} |
|
27 |
if(type.equals("ri")) { |
|
28 |
type = "community"; |
|
29 |
} |
|
30 |
return type; |
|
31 |
} |
|
32 |
|
|
33 |
/** |
|
34 |
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT |
|
35 |
* |
|
36 |
* */ |
|
37 |
public String curator(String type) { |
|
38 |
return "CURATOR_"+mapType(type).toUpperCase(); |
|
39 |
} |
|
40 |
|
|
41 |
/** |
|
42 |
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT |
|
43 |
* |
|
44 |
* Id = EE, EGI, etc |
|
45 |
* */ |
|
46 |
public String manager(String type, String id) { |
|
47 |
return mapType(type).toUpperCase() + "_" + id.toUpperCase() + "_MANAGER"; |
|
48 |
} |
|
49 |
|
|
50 |
/** |
|
51 |
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT |
|
52 |
* |
|
53 |
* Id = EE, EGI, etc |
|
54 |
* */ |
|
55 |
public String member(String type, String id) { |
|
56 |
return mapType(type).toUpperCase() + "_" + id.toUpperCase(); |
|
57 |
} |
|
58 |
|
|
59 |
public List<String> getRoles() { |
|
60 |
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
|
61 |
if(authentication != null) { |
|
62 |
List<GrantedAuthority> authorities = (List<GrantedAuthority>) authentication.getAuthorities(); |
|
63 |
if(authorities != null) { |
|
64 |
List<String> roles = new ArrayList<>(); |
|
65 |
authorities.forEach((authority) -> { |
|
66 |
roles.add(authority.getAuthority()); |
|
67 |
}); |
|
68 |
return roles; |
|
69 |
} |
|
70 |
} |
|
71 |
return null; |
|
72 |
} |
|
73 |
|
|
74 |
public String getAaiId() { |
|
75 |
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
|
76 |
if(authentication != null) { |
|
77 |
User user = (User) authentication.getPrincipal(); |
|
78 |
return user.getPassword(); |
|
79 |
} |
|
80 |
return null; |
|
81 |
} |
|
82 |
} |
modules/uoa-authorization-library/branches/redis/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/AuthorizationFilter.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.security; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaauthorizationlibrary.utils.AuthorizationUtils; |
|
4 |
import org.apache.log4j.Logger; |
|
5 |
import org.springframework.beans.factory.annotation.Autowired; |
|
6 |
import org.springframework.http.HttpStatus; |
|
7 |
import org.springframework.security.core.Authentication; |
|
8 |
import org.springframework.security.core.context.SecurityContextHolder; |
|
9 |
import org.springframework.stereotype.Component; |
|
10 |
import org.springframework.web.filter.GenericFilterBean; |
|
11 |
|
|
12 |
import javax.servlet.*; |
|
13 |
import javax.servlet.http.HttpServletRequest; |
|
14 |
import javax.servlet.http.HttpServletResponse; |
|
15 |
import java.io.IOException; |
|
16 |
|
|
17 |
@Component |
|
18 |
public class AuthorizationFilter implements Filter { |
|
19 |
|
|
20 |
private AuthorizationProvider authorizationProvider; |
|
21 |
private AuthorizationUtils utils; |
|
22 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
23 |
|
|
24 |
@Autowired |
|
25 |
AuthorizationFilter(AuthorizationProvider authorizationProvider, AuthorizationUtils utils) { |
|
26 |
this.authorizationProvider = authorizationProvider; |
|
27 |
this.utils = utils; |
|
28 |
} |
|
29 |
|
|
30 |
@Override |
|
31 |
public void init(FilterConfig filterConfig) throws ServletException { |
|
32 |
|
|
33 |
} |
|
34 |
|
|
35 |
@Override |
|
36 |
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException { |
|
37 |
HttpServletRequest request = (HttpServletRequest) req; |
|
38 |
String token = utils.getToken(request); |
|
39 |
if (token != null) { |
|
40 |
Authentication auth = authorizationProvider.getAuthentication(token); |
|
41 |
if(auth != null) { |
|
42 |
SecurityContextHolder.getContext().setAuthentication(auth); |
|
43 |
} |
|
44 |
} |
|
45 |
filterChain.doFilter(req, res); |
|
46 |
} |
|
47 |
|
|
48 |
@Override |
|
49 |
public void destroy() { |
|
50 |
|
|
51 |
} |
|
52 |
} |
modules/uoa-authorization-library/branches/redis/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/UserDetailsServiceImpl.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.security; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaauthorizationlibrary.utils.AuthorizationUtils; |
|
4 |
import eu.dnetlib.uoaauthorizationlibrary.utils.UserInfo; |
|
5 |
import org.springframework.beans.factory.annotation.Autowired; |
|
6 |
import org.springframework.security.core.GrantedAuthority; |
|
7 |
import org.springframework.security.core.authority.SimpleGrantedAuthority; |
|
8 |
import org.springframework.security.core.userdetails.UserDetails; |
|
9 |
import org.springframework.security.core.userdetails.UserDetailsService; |
|
10 |
import org.springframework.security.core.userdetails.UsernameNotFoundException; |
|
11 |
import org.springframework.stereotype.Service; |
|
12 |
|
|
13 |
import java.util.ArrayList; |
|
14 |
import java.util.List; |
|
15 |
|
|
16 |
@Service |
|
17 |
public class UserDetailsServiceImpl implements UserDetailsService { |
|
18 |
|
|
19 |
private AuthorizationUtils utils; |
|
20 |
|
|
21 |
@Autowired |
|
22 |
public UserDetailsServiceImpl(AuthorizationUtils utils) { |
|
23 |
this.utils = utils; |
|
24 |
} |
|
25 |
|
|
26 |
private List<GrantedAuthority> getAuthorities(List<String> roles) { |
|
27 |
List<GrantedAuthority> authorities = new ArrayList<>(); |
|
28 |
for(String role : roles) { |
|
29 |
authorities.add(new SimpleGrantedAuthority(role)); |
|
30 |
} |
|
31 |
return authorities; |
|
32 |
} |
|
33 |
|
|
34 |
@Override |
|
35 |
public UserDetails loadUserByUsername(String token) throws UsernameNotFoundException { |
|
36 |
final UserInfo user = utils.getUserInfo(token); |
|
37 |
|
|
38 |
if (user == null) { |
|
39 |
throw new UsernameNotFoundException("invalid token: " + token); |
|
40 |
} |
|
41 |
|
|
42 |
return org.springframework.security.core.userdetails.User |
|
43 |
.withUsername(user.getEmail()).password(user.getSub()) |
|
44 |
.authorities(getAuthorities(user.getRoles())) |
|
45 |
.accountExpired(false) |
|
46 |
.accountLocked(false) |
|
47 |
.credentialsExpired(false) |
|
48 |
.disabled(false) |
|
49 |
.build(); |
|
50 |
} |
|
51 |
|
|
52 |
} |
modules/uoa-authorization-library/branches/redis/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/AuthorizationFilterConfigurer.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.security; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaauthorizationlibrary.utils.AuthorizationUtils; |
|
4 |
import org.springframework.beans.factory.annotation.Autowired; |
|
5 |
import org.springframework.security.config.annotation.SecurityConfigurerAdapter; |
|
6 |
import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
|
7 |
import org.springframework.security.web.DefaultSecurityFilterChain; |
|
8 |
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
|
9 |
|
|
10 |
public class AuthorizationFilterConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> { |
|
11 |
|
|
12 |
private AuthorizationProvider authorizationProvider; |
|
13 |
private AuthorizationUtils utils; |
|
14 |
|
|
15 |
@Override |
|
16 |
public void init(HttpSecurity http) throws Exception { |
|
17 |
http.csrf().disable(); |
|
18 |
} |
|
19 |
|
|
20 |
@Autowired |
|
21 |
public AuthorizationFilterConfigurer(AuthorizationProvider authorizationProvider, AuthorizationUtils utils) { |
|
22 |
this.authorizationProvider = authorizationProvider; |
|
23 |
this.utils = utils; |
|
24 |
} |
|
25 |
|
|
26 |
@Override |
|
27 |
public void configure(HttpSecurity http) throws Exception { |
|
28 |
AuthorizationFilter customFilter = new AuthorizationFilter(authorizationProvider, utils); |
|
29 |
http.addFilterBefore(customFilter, UsernamePasswordAuthenticationFilter.class); |
|
30 |
} |
|
31 |
|
|
32 |
} |
modules/uoa-authorization-library/branches/redis/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/AuthorizationProvider.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.security; |
|
2 |
|
|
3 |
import org.springframework.beans.factory.annotation.Autowired; |
|
4 |
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
|
5 |
import org.springframework.security.core.Authentication; |
|
6 |
import org.springframework.security.core.userdetails.UserDetails; |
|
7 |
import org.springframework.security.core.userdetails.UsernameNotFoundException; |
|
8 |
import org.springframework.stereotype.Component; |
|
9 |
|
|
10 |
@Component |
|
11 |
public class AuthorizationProvider { |
|
12 |
|
|
13 |
private UserDetailsServiceImpl userDetailsService; |
|
14 |
|
|
15 |
@Autowired |
|
16 |
AuthorizationProvider(UserDetailsServiceImpl userDetailsService) { |
|
17 |
this.userDetailsService = userDetailsService; |
|
18 |
} |
|
19 |
|
|
20 |
public Authentication getAuthentication(String token) { |
|
21 |
try { |
|
22 |
UserDetails userDetails = userDetailsService.loadUserByUsername(token); |
|
23 |
return new UsernamePasswordAuthenticationToken(userDetails, "", userDetails.getAuthorities()); |
|
24 |
} catch (UsernameNotFoundException e) { |
|
25 |
return null; |
|
26 |
} |
|
27 |
} |
|
28 |
} |
modules/uoa-authorization-library/branches/redis/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/EntryPoint.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.security; |
|
2 |
|
|
3 |
import org.springframework.security.core.AuthenticationException; |
|
4 |
import org.springframework.security.web.AuthenticationEntryPoint; |
|
5 |
|
|
6 |
import javax.servlet.http.HttpServletRequest; |
|
7 |
import javax.servlet.http.HttpServletResponse; |
|
8 |
import java.io.IOException; |
|
9 |
|
|
10 |
public class EntryPoint implements AuthenticationEntryPoint { |
|
11 |
|
|
12 |
@Override |
|
13 |
public void commence(HttpServletRequest request, HttpServletResponse response, |
|
14 |
AuthenticationException authException) throws IOException { |
|
15 |
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage()); |
|
16 |
} |
|
17 |
|
|
18 |
} |
|
19 |
|
modules/uoa-authorization-library/branches/redis/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/WebSecurityConfig.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.security; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaauthorizationlibrary.utils.AuthorizationUtils; |
|
4 |
import org.springframework.beans.factory.annotation.Autowired; |
|
5 |
import org.springframework.context.annotation.ComponentScan; |
|
6 |
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; |
|
7 |
import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
|
8 |
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
|
9 |
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
|
10 |
import org.springframework.security.config.http.SessionCreationPolicy; |
|
11 |
|
|
12 |
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) |
|
13 |
@EnableWebSecurity |
|
14 |
@ComponentScan(basePackages = {"eu.dnetlib.uoaauthorizationlibrary.*"}) |
|
15 |
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
|
16 |
|
|
17 |
private AuthorizationProvider authorizationProvider; |
|
18 |
private AuthorizationUtils utils; |
|
19 |
|
|
20 |
@Autowired |
|
21 |
WebSecurityConfig(AuthorizationProvider authorizationProvider, AuthorizationUtils utils) { |
|
22 |
this.authorizationProvider = authorizationProvider; |
|
23 |
this.utils = utils; |
|
24 |
} |
|
25 |
|
|
26 |
@Override |
|
27 |
protected void configure(HttpSecurity http) throws Exception { |
|
28 |
http.apply(new AuthorizationFilterConfigurer(authorizationProvider, utils)); |
|
29 |
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); |
|
30 |
http.authorizeRequests().anyRequest().permitAll(); |
|
31 |
http.httpBasic().authenticationEntryPoint(new EntryPoint()); |
|
32 |
} |
|
33 |
|
|
34 |
} |
modules/uoa-authorization-library/branches/redis/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/CorsConfig.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.security; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaauthorizationlibrary.configuration.SecurityConfig; |
|
4 |
import org.springframework.beans.factory.annotation.Autowired; |
|
5 |
import org.springframework.context.annotation.Configuration; |
|
6 |
import org.springframework.web.servlet.config.annotation.CorsRegistry; |
|
7 |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; |
|
8 |
|
|
9 |
@Configuration |
|
10 |
public class CorsConfig extends WebMvcConfigurerAdapter { |
|
11 |
|
|
12 |
SecurityConfig securityConfig; |
|
13 |
|
|
14 |
@Autowired |
|
15 |
CorsConfig(SecurityConfig securityConfig) { |
|
16 |
this.securityConfig = securityConfig; |
|
17 |
} |
|
18 |
|
|
19 |
@Override |
|
20 |
public void addCorsMappings(CorsRegistry registry) { |
|
21 |
registry.addMapping("/**") |
|
22 |
.allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS") |
|
23 |
.allowCredentials(true); |
|
24 |
} |
|
25 |
} |
modules/uoa-authorization-library/branches/redis/src/main/resources/log4j.properties | ||
---|---|---|
1 |
log4j.rootLogger = DEBUG, R |
|
2 |
|
|
3 |
log4j.logger.eu.dnetlib = DEBUG |
|
4 |
log4j.logger.org.springframework = DEBUG, S |
|
5 |
|
|
6 |
log4j.additivity.org.springframework = false |
|
7 |
|
|
8 |
log4j.appender.R=org.apache.log4j.RollingFileAppender |
|
9 |
log4j.appender.R.File=/var/log/dnet/uoa-authorization-library/uoa-authorization-library.log |
|
10 |
log4j.appender.R.MaxFileSize=10MB |
|
11 |
log4j.appender.R.MaxBackupIndex=10 |
|
12 |
log4j.appender.R.layout=org.apache.log4j.PatternLayout |
|
13 |
log4j.appender.R.layout.ConversionPattern= %d %p %t [%c] - %m%n |
|
14 |
|
|
15 |
log4j.appender.S=org.apache.log4j.RollingFileAppender |
|
16 |
log4j.appender.S.File=/var/log/dnet/uoa-authorization-library/uoa-authorization-library-spring.log |
|
17 |
log4j.appender.S.MaxFileSize=10MB |
|
18 |
log4j.appender.S.MaxBackupIndex=10 |
|
19 |
log4j.appender.S.layout=org.apache.log4j.PatternLayout |
|
20 |
log4j.appender.S.layout.ConversionPattern= %d %p %t [%c] - %m%n |
modules/uoa-authorization-library/branches/redis/src/main/resources/authorization.properties | ||
---|---|---|
1 |
#dev |
|
2 |
authorization.security.userInfoUrl = http://mpagasas.di.uoa.gr:8080/dnet-openaire-users-1.0.0-SNAPSHOT/api/users/getUserInfo?accessToken= |
|
3 |
authorization.security.originServer = di.uoa.gr |
|
4 |
|
|
5 |
#beta |
|
6 |
#authorization.security.userInfoUrl = https://beta.services.openaire.eu/uoa-user-management/api/users/getUserInfo?accessToken= |
|
7 |
#authorization.security.originServer = .openaire.eu |
|
8 |
|
|
9 |
|
|
10 |
#production |
|
11 |
#authorization.security.userInfoUrl = https://services.openaire.eu/uoa-user-management/api/users/getUserInfo?accessToken= |
|
12 |
#authorization.security.originServer = .openaire.eu |
modules/uoa-authorization-library/branches/redis/src/main/resources/application.properties | ||
---|---|---|
1 |
server.port=9090 |
modules/uoa-authorization-library/branches/redis/pom.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|
3 |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
4 |
<modelVersion>4.0.0</modelVersion> |
|
5 |
|
|
6 |
<groupId>eu.dnetlib</groupId> |
|
7 |
<artifactId>uoa-authorization-library</artifactId> |
|
8 |
<version>1.0.0-SNAPSHOT</version> |
|
9 |
<packaging>jar</packaging> |
|
10 |
|
|
11 |
<name>uoa-authorization-library</name> |
|
12 |
|
|
13 |
<parent> |
|
14 |
<groupId>org.springframework.boot</groupId> |
|
15 |
<artifactId>spring-boot-starter-parent</artifactId> |
|
16 |
<version>1.5.8.RELEASE</version> |
|
17 |
<relativePath/> <!-- lookup parent from repository --> |
|
18 |
</parent> |
|
19 |
|
|
20 |
<properties> |
|
21 |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
|
22 |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> |
|
23 |
<java.version>1.8</java.version> |
|
24 |
</properties> |
|
25 |
|
|
26 |
<dependencies> |
|
27 |
<dependency> |
|
28 |
<groupId>org.springframework.boot</groupId> |
|
29 |
<artifactId>spring-boot-starter-web</artifactId> |
|
30 |
<exclusions> |
|
31 |
<exclusion> |
|
32 |
<groupId> org.springframework.boot</groupId> |
|
33 |
<artifactId>spring-boot-starter-logging</artifactId> |
|
34 |
</exclusion> |
|
35 |
</exclusions> |
|
36 |
</dependency> |
|
37 |
<!-- Starter for using Spring Security --> |
|
38 |
<dependency> |
|
39 |
<groupId>org.springframework.boot</groupId> |
|
40 |
<artifactId>spring-boot-starter-security</artifactId> |
|
41 |
</dependency> |
|
42 |
<dependency> |
|
43 |
<groupId>com.google.code.gson</groupId> |
|
44 |
<artifactId>gson</artifactId> |
|
45 |
<version>2.8.2</version> |
|
46 |
</dependency> |
|
47 |
<dependency> |
|
48 |
<groupId>log4j</groupId> |
|
49 |
<artifactId>log4j</artifactId> |
|
50 |
<version>1.2.17</version> |
|
51 |
</dependency> |
|
52 |
</dependencies> |
|
53 |
<build> |
|
54 |
<plugins> |
|
55 |
</plugins> |
|
56 |
<finalName>uoa-authorization-library</finalName> |
|
57 |
</build> |
|
58 |
</project> |
Also available in: Unified diff
[Authorization library: Creating branch for redis integration]