1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
package capteur.administration.web.services;
import java.io.IOException;
import nu.localhost.tapestry5.springsecurity.services.RequestInvocationDefinition;
import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.ioc.Configuration;
import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.ioc.OrderedConfiguration;
import org.apache.tapestry5.ioc.annotations.Contribute;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.InjectService;
import org.apache.tapestry5.ioc.annotations.Local;
import org.apache.tapestry5.ioc.services.ServiceOverride;
import org.apache.tapestry5.ioc.services.SymbolProvider;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.RequestFilter;
import org.apache.tapestry5.services.RequestHandler;
import org.apache.tapestry5.services.Response;
import org.slf4j.Logger;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.SaltSource;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
import org.springframework.security.core.userdetails.UserDetailsService;
import capteur.administration.web.services.security.UserDetailsServiceImpl;
import capteur.administration.web.utils.PropertiesFileSymbolProvider;
/**
* This module is automatically included as part of the Tapestry IoC Registry,
* it's a good place to configure and extend Tapestry, or to place your own
* service definitions.
*/
public class AppModule {
public static UserDetailsService buildUserDetailsService(
@Inject PasswordEncoder encoder, @Inject SaltSource salt) {
return new UserDetailsServiceImpl(encoder, salt);
}
public static void contributeProviderManager(
OrderedConfiguration<AuthenticationProvider> configuration,
@InjectService("DaoAuthenticationProvider") AuthenticationProvider daoAuthenticationProvider) {
configuration.add("daoAuthenticationProvider",
daoAuthenticationProvider);
}
public static void contributeFilterSecurityInterceptor(
Configuration<RequestInvocationDefinition> configuration) {
configuration.add(new RequestInvocationDefinition(
"/home/**", "ROLE_USER "));
configuration.add(new RequestInvocationDefinition("/Statistic/**",
"ROLE_ADMIN"));
}
@Contribute(ServiceOverride.class)
public static void setupApplicationServiceOverrides(
MappedConfiguration<Class, Object> configuration) {
configuration.add(PasswordEncoder.class, new ShaPasswordEncoder());
}
public static void contributeFactoryDefaults(
MappedConfiguration<String, Object> configuration) {
// The application version number is incorprated into URLs for some
// assets. Web browsers will cache assets because of the far future
// expires
// header. If existing assets are changed, the version number should
// also
// change, to force the browser to download new versions. This overrides
// Tapesty's default
// (a random hexadecimal number), but may be further overriden by
// DevelopmentModule or
// QaModule.
configuration.override(SymbolConstants.APPLICATION_VERSION,
"1.0-SNAPSHOT");
}
public static void contributeApplicationDefaults(
MappedConfiguration<String, Object> configuration) {
// Contributions to ApplicationDefaults will override any contributions
// to
// FactoryDefaults (with the same key). Here we're restricting the
// supported
// locales to just "en" (English). As you add localised message catalogs
// and other assets,
// you can extend this list of locales (it's a comma separated series of
// locale names;
// the first locale name is the default when there's no reasonable
// match).
configuration.add("tapestry.start-page-name", "Login");
configuration.add("tapestry.compress-whitespace", "false");
configuration.add("tapestry.production-mode", "false");
configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en,fr");
configuration.add("spring-security.failure.url", "/login/failed");
configuration.add("spring-security.accessDenied.url", "/login/failed");
configuration.add("spring-security.check.url",
"/j_spring_security_check");
configuration.add("spring-security.target.url",
"/home");
configuration.add("spring-security.afterlogout.url", "/login");
configuration.add("spring-security.rememberme.key", "REMEMBERMEKEY");
configuration.add("spring-security.loginform.url", "/login");
configuration.add("spring-security.force.ssl.login", "false");
configuration.add("spring-security.anonymous.key", "acegi_anonymous");
configuration.add("spring-security.anonymous.attribute",
"anonymous,ROLE_ANONYMOUS");
configuration.add("spring-security.password.salt", "DEADBEEF");
}
/**
* This is a service definition, the service will be named "TimingFilter".
* The interface, RequestFilter, is used within the RequestHandler service
* pipeline, which is built from the RequestHandler service configuration.
* Tapestry IoC is responsible for passing in an appropriate Logger
* instance. Requests for static resources are handled at a higher level, so
* this filter will only be invoked for Tapestry related requests.
* <p/>
* <p/>
* Service builder methods are useful when the implementation is inline as
* an inner class (as here) or require some other kind of special
* initialization. In most cases, use the static bind() method instead.
* <p/>
* <p/>
* If this method was named "build", then the service id would be taken from
* the service interface and would be "RequestFilter". Since Tapestry
* already defines a service named "RequestFilter" we use an explicit
* service id that we can reference inside the contribution method.
*/
public RequestFilter buildTimingFilter(final Logger log) {
return new RequestFilter() {
public boolean service(Request request, Response response,
RequestHandler handler) throws IOException {
long startTime = System.currentTimeMillis();
try {
// The responsibility of a filter is to invoke the
// corresponding method
// in the handler. When you chain multiple filters together,
// each filter
// received a handler that is a bridge to the next filter.
return handler.service(request, response);
} finally {
long elapsed = System.currentTimeMillis() - startTime;
log.info(String.format("Request time: %d ms", elapsed));
}
}
};
}
/**
* This is a contribution to the RequestHandler service configuration. This
* is how we extend Tapestry using the timing filter. A common use for this
* kind of filter is transaction management or security. The @Local
* annotation selects the desired service by type, but only from the same
* module. Without @Local, there would be an error due to the other
* service(s) that implement RequestFilter (defined in other modules).
*/
public void contributeRequestHandler(
OrderedConfiguration<RequestFilter> configuration,
@Local RequestFilter filter) {
// Each contribution to an ordered configuration has a name, When
// necessary, you may
// set constraints to precisely control the invocation order of the
// contributed filter
// within the pipeline.
configuration.add("Timing", filter);
}
// public static void contributeSymbolSource(
// OrderedConfiguration<SymbolProvider> providers) {
// providers.add("springSecurity", new ClasspathResourceSymbolProvider(
// "security.properties"));
//
// }
// make configuration from 'site.properties' on the filesystem available
// as symbols
public PropertiesFileSymbolProvider buildClasspathPropertiesFileSymbolProvider(
Logger logger) {
return new PropertiesFileSymbolProvider(logger, "site.properties",
true);
}
public static void contributeSymbolSource(
OrderedConfiguration<SymbolProvider> configuration,
@InjectService("ClasspathPropertiesFileSymbolProvider") SymbolProvider classpathPropertiesFileSymbolProvider)
{
configuration.add("ClasspathPropertiesFile",
classpathPropertiesFileSymbolProvider,
"after:SystemProperties", "before:ApplicationDefaults");
}
} |