Revize 32f56e61
Přidáno uživatelem Jiri Trefil před asi 2 roky(ů)
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/security/WebSecurityConfig.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.security; |
|
2 |
|
|
3 |
import org.springframework.beans.factory.annotation.Autowired; |
|
4 |
import org.springframework.context.annotation.Bean; |
|
5 |
import org.springframework.security.authentication.AuthenticationManager; |
|
6 |
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
|
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 |
import org.springframework.security.core.userdetails.UserDetailsService; |
|
12 |
|
|
13 |
@EnableWebSecurity |
|
14 |
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
|
15 |
|
|
16 |
@Autowired |
|
17 |
private UserDetailsService userService; |
|
18 |
|
|
19 |
@Autowired |
|
20 |
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { |
|
21 |
auth.userDetailsService(userService); |
|
22 |
} |
|
23 |
|
|
24 |
|
|
25 |
@Bean |
|
26 |
public UserDetailsService userDetailsService() { |
|
27 |
return super.userDetailsService(); |
|
28 |
} |
|
29 |
|
|
30 |
@Override |
|
31 |
@Bean |
|
32 |
public AuthenticationManager authenticationManagerBean() throws Exception { |
|
33 |
return super.authenticationManagerBean(); |
|
34 |
} |
|
35 |
|
|
36 |
@Override |
|
37 |
protected void configure(HttpSecurity httpSecurity) throws Exception { |
|
38 |
httpSecurity.csrf().disable() |
|
39 |
.authorizeRequests().antMatchers("/v2/user/register","/v2/user/login").permitAll(). |
|
40 |
anyRequest().authenticated().and(). |
|
41 |
exceptionHandling().and().sessionManagement() |
|
42 |
.sessionCreationPolicy(SessionCreationPolicy.STATELESS); |
|
43 |
// httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class); |
|
44 |
|
|
45 |
} |
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/OAuthService.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service; |
|
2 |
|
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
|
4 |
import org.springframework.http.ResponseEntity; |
|
5 |
|
|
6 |
public interface OAuthService { |
|
7 |
public ResponseEntity<String> authenticate(User user); |
|
8 |
public ResponseEntity<String> loginUser(User user); |
|
9 |
|
|
10 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/OAuthServiceImpl.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service; |
|
2 |
|
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
|
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.RequestBuilder; |
|
5 |
import org.springframework.beans.factory.annotation.Autowired; |
|
6 |
import org.springframework.beans.factory.annotation.Value; |
|
7 |
import org.springframework.http.ResponseEntity; |
|
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 |
import java.util.ArrayList; |
|
13 |
import java.util.HashMap; |
|
14 |
|
|
15 |
@Service |
|
16 |
public class OAuthServiceImpl implements OAuthService, UserDetailsService { |
|
17 |
|
|
18 |
|
|
19 |
@Value("${auth.realm.authenticate}") |
|
20 |
private String AUTH_URL_AUTH; |
|
21 |
|
|
22 |
@Value("${auth.realm.login}") |
|
23 |
private String AUTH_URL_LOGIN; |
|
24 |
|
|
25 |
@Autowired |
|
26 |
private UserService userService; |
|
27 |
|
|
28 |
public ResponseEntity<String> authenticate(User user) { |
|
29 |
final String userName = user.getName(); |
|
30 |
final String token = user.getToken(); |
|
31 |
|
|
32 |
if(userName == null || token == null) { |
|
33 |
return null; |
|
34 |
} |
|
35 |
|
|
36 |
//HttpURLConnection con = RequestBuilder.createConnection(AUTH_URL); |
|
37 |
HashMap<String, String> requestBody = new HashMap<>(); |
|
38 |
|
|
39 |
requestBody.put("name", userName); |
|
40 |
requestBody.put("token", token); |
|
41 |
|
|
42 |
ResponseEntity<String> response = RequestBuilder.sendRequestResponse(AUTH_URL_AUTH, requestBody); |
|
43 |
|
|
44 |
return response; |
|
45 |
} |
|
46 |
|
|
47 |
public ResponseEntity<String> loginUser(User user) { |
|
48 |
final String userName = user.getName(); |
|
49 |
|
|
50 |
if(userName == null) { |
|
51 |
return null; |
|
52 |
} |
|
53 |
//HttpURLConnection con = RequestBuilder.createConnection(AUTH_URL); |
|
54 |
HashMap<String, String> requestBody = new HashMap<>(); |
|
55 |
|
|
56 |
requestBody.put("name", userName); |
|
57 |
|
|
58 |
ResponseEntity<String> response = RequestBuilder.sendRequestResponse(AUTH_URL_LOGIN, requestBody); |
|
59 |
|
|
60 |
return response; |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { |
|
65 |
final User user = this.userService.getUserByName(s); |
|
66 |
return new org.springframework.security.core.userdetails.User(user.getName(),user.getPassword(),new ArrayList<>()); |
|
67 |
} |
|
68 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/ParamsBuilder.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils; |
|
2 |
|
|
3 |
import java.io.UnsupportedEncodingException; |
|
4 |
import java.net.URLEncoder; |
|
5 |
import java.util.HashMap; |
|
6 |
|
|
7 |
public class ParamsBuilder { |
|
8 |
|
|
9 |
/** |
|
10 |
* Method creates body of post request |
|
11 |
* ie returns json representation of parameters |
|
12 |
* @param parameters Key: value pair parameters |
|
13 |
* @return String JSON representation |
|
14 |
*/ |
|
15 |
public static String createPostParams(HashMap<String,String> parameters) { |
|
16 |
return JSONBuilder.buildJson(parameters); |
|
17 |
} |
|
18 |
|
|
19 |
/** |
|
20 |
* Method creates get parameters from parameters |
|
21 |
* @param params parameters |
|
22 |
* @return String GET representation of parameters passed into the function |
|
23 |
* @throws UnsupportedEncodingException if unknown encoding standard is passed as an argument to the function |
|
24 |
*/ |
|
25 |
public static String createGetParams(HashMap<String, String> params) throws UnsupportedEncodingException { |
|
26 |
StringBuilder builder = new StringBuilder(); |
|
27 |
for (HashMap.Entry<String, String> entry : params.entrySet()) { |
|
28 |
builder.append(URLEncoder.encode(entry.getKey(), "UTF-8")); |
|
29 |
builder.append("="); |
|
30 |
builder.append(URLEncoder.encode(entry.getValue(), "UTF-8")); |
|
31 |
builder.append("&"); |
|
32 |
} |
|
33 |
|
|
34 |
String resultString = builder.toString(); |
|
35 |
return resultString.length() > 0 |
|
36 |
? resultString.substring(0, resultString.length() - 1) |
|
37 |
: resultString; |
|
38 |
|
|
39 |
} |
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/RequestBuilder.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils; |
|
2 |
|
|
3 |
import org.springframework.web.client.RestTemplate; |
|
4 |
|
|
5 |
import java.util.HashMap; |
|
6 |
import java.util.logging.Logger; |
|
7 |
|
|
8 |
import org.springframework.http.*; |
|
9 |
|
|
10 |
public class RequestBuilder { |
|
11 |
|
|
12 |
|
|
13 |
private static Logger logger = Logger.getLogger(RequestBuilder.class.getName()); |
|
14 |
|
|
15 |
|
|
16 |
public static ResponseEntity<String> sendRequestResponse(String url, HashMap<String,String> body) { |
|
17 |
RestTemplate restTemplate = new RestTemplate(); |
|
18 |
String json = JSONBuilder.buildJson(body); |
|
19 |
|
|
20 |
HttpHeaders headers = new HttpHeaders(); |
|
21 |
headers.setContentType(MediaType.APPLICATION_JSON); |
|
22 |
HttpEntity<String> entity = new HttpEntity<>(json, headers); |
|
23 |
ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class); |
|
24 |
|
|
25 |
return response; |
|
26 |
} |
|
27 |
|
|
28 |
} |
Také k dispozici: Unified diff
#10228 definice realmu v application properties souboru