|
1 | | -package com.naturalprogrammer.spring.lemon.security; |
2 | | - |
3 | | -import org.apache.commons.logging.Log; |
4 | | -import org.apache.commons.logging.LogFactory; |
5 | | -import org.springframework.beans.factory.annotation.Autowired; |
6 | | -import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
7 | | -import org.springframework.security.crypto.password.PasswordEncoder; |
8 | | -import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; |
9 | | -import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
10 | | - |
11 | | -import com.naturalprogrammer.spring.lemon.commons.LemonProperties; |
12 | | -import com.naturalprogrammer.spring.lemon.commonsweb.security.LemonWebSecurityConfig; |
13 | | - |
14 | | -/** |
15 | | - * Security configuration class. Extend it in the |
16 | | - * application, and make a configuration class. Override |
17 | | - * protected methods, if you need any customization. |
18 | | - * |
19 | | - * @author Sanjay Patel |
20 | | - */ |
21 | | -public class LemonJpaSecurityConfig extends LemonWebSecurityConfig { |
22 | | - |
23 | | - private static final Log log = LogFactory.getLog(LemonJpaSecurityConfig.class); |
24 | | - |
25 | | - private LemonProperties properties; |
26 | | - private LemonUserDetailsService<?, ?> userDetailsService; |
27 | | - private LemonAuthenticationSuccessHandler authenticationSuccessHandler; |
28 | | - private LemonOidcUserService oidcUserService; |
29 | | - private LemonOAuth2UserService<?, ?> oauth2UserService; |
30 | | - private OAuth2AuthenticationSuccessHandler<?> oauth2AuthenticationSuccessHandler; |
31 | | - private OAuth2AuthenticationFailureHandler oauth2AuthenticationFailureHandler; |
32 | | - private PasswordEncoder passwordEncoder; |
33 | | - |
34 | | - @Autowired |
35 | | - public void createLemonSecurityConfig(LemonProperties properties, LemonUserDetailsService<?, ?> userDetailsService, |
36 | | - LemonAuthenticationSuccessHandler authenticationSuccessHandler, |
37 | | - LemonOidcUserService oidcUserService, |
38 | | - LemonOAuth2UserService<?, ?> oauth2UserService, |
39 | | - OAuth2AuthenticationSuccessHandler<?> oauth2AuthenticationSuccessHandler, |
40 | | - OAuth2AuthenticationFailureHandler oauth2AuthenticationFailureHandler, |
41 | | - PasswordEncoder passwordEncoder) { |
42 | | - |
43 | | - this.properties = properties; |
44 | | - this.userDetailsService = userDetailsService; |
45 | | - this.authenticationSuccessHandler = authenticationSuccessHandler; |
46 | | - this.oidcUserService = oidcUserService; |
47 | | - this.oauth2UserService = oauth2UserService; |
48 | | - this.oauth2AuthenticationSuccessHandler = oauth2AuthenticationSuccessHandler; |
49 | | - this.oauth2AuthenticationFailureHandler = oauth2AuthenticationFailureHandler; |
50 | | - this.passwordEncoder = passwordEncoder; |
51 | | - |
52 | | - log.info("Created"); |
53 | | - } |
54 | | - |
55 | | - /** |
56 | | - * Security configuration, calling protected methods |
57 | | - */ |
58 | | - @Override |
59 | | - protected void configure(HttpSecurity http) throws Exception { |
60 | | - |
61 | | - super.configure(http); |
62 | | - login(http); // authentication |
63 | | - exceptionHandling(http); // exception handling |
64 | | - oauth2Client(http); |
65 | | - } |
66 | | - |
67 | | - |
68 | | - /** |
69 | | - * Configuring authentication. |
70 | | - */ |
71 | | - protected void login(HttpSecurity http) throws Exception { |
72 | | - |
73 | | - http |
74 | | - .formLogin() // form login |
75 | | - .loginPage(loginPage()) |
76 | | - |
77 | | - /****************************************** |
78 | | - * Setting a successUrl would redirect the user there. Instead, |
79 | | - * let's send 200 and the userDto along with an Authorization token. |
80 | | - *****************************************/ |
81 | | - .successHandler(authenticationSuccessHandler) |
82 | | - |
83 | | - /******************************************* |
84 | | - * Setting the failureUrl will redirect the user to |
85 | | - * that url if login fails. Instead, we need to send |
86 | | - * 401. So, let's set failureHandler instead. |
87 | | - *******************************************/ |
88 | | - .failureHandler(new SimpleUrlAuthenticationFailureHandler()); |
89 | | - } |
90 | | - |
91 | | - |
92 | | - /** |
93 | | - * Override this to change login URL |
94 | | - * |
95 | | - * @return |
96 | | - */ |
97 | | - protected String loginPage() { |
98 | | - |
99 | | - return "/api/core/login"; |
100 | | - } |
101 | | - |
102 | | - |
103 | | - protected void oauth2Client(HttpSecurity http) throws Exception { |
104 | | - |
105 | | - http.oauth2Login() |
106 | | - .authorizationEndpoint() |
107 | | - .authorizationRequestRepository(new HttpCookieOAuth2AuthorizationRequestRepository(properties)).and() |
108 | | - .successHandler(oauth2AuthenticationSuccessHandler) |
109 | | - .failureHandler(oauth2AuthenticationFailureHandler) |
110 | | - .userInfoEndpoint() |
111 | | - .oidcUserService(oidcUserService) |
112 | | - .userService(oauth2UserService); |
113 | | - } |
114 | | - |
115 | | - |
116 | | - /** |
117 | | - * Configuring token authentication filter |
118 | | - */ |
119 | | - protected void tokenAuthentication(HttpSecurity http) throws Exception { |
120 | | - |
121 | | - http.addFilterBefore(new LemonJpaTokenAuthenticationFilter(blueTokenService, userDetailsService), |
122 | | - UsernamePasswordAuthenticationFilter.class); |
123 | | - } |
124 | | -} |
| 1 | +package com.naturalprogrammer.spring.lemon.security; |
| 2 | + |
| 3 | +import org.apache.commons.logging.Log; |
| 4 | +import org.apache.commons.logging.LogFactory; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 7 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 8 | +import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; |
| 9 | +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
| 10 | + |
| 11 | +import com.naturalprogrammer.spring.lemon.commons.LemonProperties; |
| 12 | +import com.naturalprogrammer.spring.lemon.commonsweb.security.LemonWebSecurityConfig; |
| 13 | + |
| 14 | +/** |
| 15 | + * Security configuration class. Extend it in the |
| 16 | + * application, and make a configuration class. Override |
| 17 | + * protected methods, if you need any customization. |
| 18 | + * |
| 19 | + * @author Sanjay Patel |
| 20 | + */ |
| 21 | +public class LemonJpaSecurityConfig extends LemonWebSecurityConfig { |
| 22 | + |
| 23 | + private static final Log log = LogFactory.getLog(LemonJpaSecurityConfig.class); |
| 24 | + |
| 25 | + private LemonProperties properties; |
| 26 | + private LemonUserDetailsService<?, ?> userDetailsService; |
| 27 | + private LemonAuthenticationSuccessHandler authenticationSuccessHandler; |
| 28 | + private LemonOidcUserService oidcUserService; |
| 29 | + private LemonOAuth2UserService<?, ?> oauth2UserService; |
| 30 | + private OAuth2AuthenticationSuccessHandler<?> oauth2AuthenticationSuccessHandler; |
| 31 | + private OAuth2AuthenticationFailureHandler oauth2AuthenticationFailureHandler; |
| 32 | + private PasswordEncoder passwordEncoder; |
| 33 | + |
| 34 | + @Autowired |
| 35 | + public void createLemonSecurityConfig(LemonProperties properties, LemonUserDetailsService<?, ?> userDetailsService, |
| 36 | + LemonAuthenticationSuccessHandler authenticationSuccessHandler, |
| 37 | + LemonOidcUserService oidcUserService, |
| 38 | + LemonOAuth2UserService<?, ?> oauth2UserService, |
| 39 | + OAuth2AuthenticationSuccessHandler<?> oauth2AuthenticationSuccessHandler, |
| 40 | + OAuth2AuthenticationFailureHandler oauth2AuthenticationFailureHandler, |
| 41 | + PasswordEncoder passwordEncoder) { |
| 42 | + |
| 43 | + this.properties = properties; |
| 44 | + this.userDetailsService = userDetailsService; |
| 45 | + this.authenticationSuccessHandler = authenticationSuccessHandler; |
| 46 | + this.oidcUserService = oidcUserService; |
| 47 | + this.oauth2UserService = oauth2UserService; |
| 48 | + this.oauth2AuthenticationSuccessHandler = oauth2AuthenticationSuccessHandler; |
| 49 | + this.oauth2AuthenticationFailureHandler = oauth2AuthenticationFailureHandler; |
| 50 | + this.passwordEncoder = passwordEncoder; |
| 51 | + |
| 52 | + log.info("Created"); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Security configuration, calling protected methods |
| 57 | + */ |
| 58 | + @Override |
| 59 | + protected void configure(HttpSecurity http) throws Exception { |
| 60 | + |
| 61 | + super.configure(http); |
| 62 | + login(http); // authentication |
| 63 | + exceptionHandling(http); // exception handling |
| 64 | + oauth2Client(http); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + /** |
| 69 | + * Configuring authentication. |
| 70 | + */ |
| 71 | + protected void login(HttpSecurity http) throws Exception { |
| 72 | + |
| 73 | + http |
| 74 | + .formLogin() // form login |
| 75 | + .loginPage(loginPage()) |
| 76 | + |
| 77 | + /****************************************** |
| 78 | + * Setting a successUrl would redirect the user there. Instead, |
| 79 | + * let's send 200 and the userDto along with an Authorization token. |
| 80 | + *****************************************/ |
| 81 | + .successHandler(authenticationSuccessHandler) |
| 82 | + |
| 83 | + /******************************************* |
| 84 | + * Setting the failureUrl will redirect the user to |
| 85 | + * that url if login fails. Instead, we need to send |
| 86 | + * 401. So, let's set failureHandler instead. |
| 87 | + *******************************************/ |
| 88 | + .failureHandler(new SimpleUrlAuthenticationFailureHandler()); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /** |
| 93 | + * Override this to change login URL |
| 94 | + * |
| 95 | + * @return |
| 96 | + */ |
| 97 | + protected String loginPage() { |
| 98 | + |
| 99 | + return properties.getLoginUrl(); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + protected void oauth2Client(HttpSecurity http) throws Exception { |
| 104 | + |
| 105 | + http.oauth2Login() |
| 106 | + .authorizationEndpoint() |
| 107 | + .authorizationRequestRepository(new HttpCookieOAuth2AuthorizationRequestRepository(properties)).and() |
| 108 | + .successHandler(oauth2AuthenticationSuccessHandler) |
| 109 | + .failureHandler(oauth2AuthenticationFailureHandler) |
| 110 | + .userInfoEndpoint() |
| 111 | + .oidcUserService(oidcUserService) |
| 112 | + .userService(oauth2UserService); |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + /** |
| 117 | + * Configuring token authentication filter |
| 118 | + */ |
| 119 | + @Override |
| 120 | + protected void tokenAuthentication(HttpSecurity http) throws Exception { |
| 121 | + |
| 122 | + http.addFilterBefore(new LemonJpaTokenAuthenticationFilter(blueTokenService, userDetailsService), |
| 123 | + UsernamePasswordAuthenticationFilter.class); |
| 124 | + } |
| 125 | +} |
0 commit comments