Hello Brian,
I configured filterRegistration wrong. I found a sample in internet and now the
problem is solved. Thank you.
--
Best regards, Alex Orlov
>Понедельник, 23 ноября 2020, 23:46 +03:00 от Brian Demers
><[email protected]>:
>
>Have you tried without setting those scopes?
>
>I would guess setting those scope _shouldn't_ matter, as the default should be
>a Singleton.
>
>If that doesn't help can you create a simple project that reproduces the
>problem on GitHub?
>On Sat, Nov 21, 2020 at 5:38 PM Alex Orlov < [email protected] > wrote:
>>Hello all,
>>
>>I try to configure Shiro web + Spring + my custrom Realm but I have a problem.
>>
>>This is my web initializer:
>>
>>@Order(Ordered.HIGHEST_PRECEDENCE)
>>public class WebInitializer implements WebApplicationInitializer {
>> @Override
>> public void onStartup(ServletContext servletContext) throws
>>ServletException {
>> var diContext = new AnnotationConfigWebApplicationContext();
>> diContext.register(TempConfig.class);
>> diContext.setServletContext(servletContext);
>> var servlet = servletContext.addServlet("SpringServlet", new
>>DispatcherServlet(diContext));
>> servlet.setLoadOnStartup(1);
>> servlet.addMapping("/");
>>
>> FilterRegistration.Dynamic filterRegistration =
>> servletContext.addFilter("ShiroFilter",
>>"org.apache.shiro.web.servlet.ShiroFilter");
>> filterRegistration.addMappingForUrlPatterns(
>> EnumSet.<DispatcherType> of(
>> DispatcherType.REQUEST,
>> DispatcherType.FORWARD,
>> DispatcherType.INCLUDE,
>> DispatcherType.ERROR
>> ),
>> false, "/*");
>>
>>servletContext.addListener("org.apache.shiro.web.env.EnvironmentLoaderListener");
>> }
>>}
>>
>>This is Spring config:
>>
>>@Configuration
>>@ComponentScan(basePackageClasses = {
>> MyController.class,
>>})
>>@EnableWebMvc
>>@Import ({
>> ShiroBeanConfiguration.class,
>> ShiroAnnotationProcessorConfiguration.class,
>> ShiroWebConfiguration.class,
>> ShiroWebFilterConfiguration.class,
>> ShiroRequestMappingConfig.class
>>})
>>public class TempConfig implements WebMvcConfigurer {
>> public TempConfig() {
>> }
>>
>> @Bean
>> @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
>> public CacheManager cacheManager() {
>> var manager = new MemoryConstrainedCacheManager();
>> return manager;
>> }
>>
>> @Bean
>> @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
>> public Realm realm() {
>> var realm = new SecurityRealm();// <- this is my custom realm.
>> return realm;
>> }
>>}
>>
>>And this is what I get:
>>
>>WARN org.apache.shiro.authc.AbstractAuthenticator - Authentication failed
>>for token submission [temp.security.WebAuthenticationToken@79ffcf2c].
>>Possible unexpected error? (Typical or expected login exceptions should
>>extend from AuthenticationException).
>>java.lang.IllegalStateException: Configuration error: No realms have been
>>configured! One or more realms must be present to execute an authentication
>>attempt.
>> at
>>org.apache.shiro.authc.pam.ModularRealmAuthenticator.assertRealmsConfigured(ModularRealmAuthenticator.java:161)
>> ~[shiro-all-1.7.0.jar:?]
>> at
>>org.apache.shiro.authc.pam.ModularRealmAuthenticator.doAuthenticate(ModularRealmAuthenticator.java:270)
>> ~[shiro-all-1.7.0.jar:?]
>> at
>>org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:198)
>> [shiro-all-1.7.0.jar:?]
>> at
>>org.apache.shiro.mgt.AuthenticatingSecurityManager.authenticate(AuthenticatingSecurityManager.java:106)
>> [shiro-all-1.7.0.jar:?]
>> at
>>org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:275)
>> [shiro-all-1.7.0.jar:?]
>> at
>>org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:260)
>> [shiro-all-1.7.0.jar:?]
>>
>>At the same time, I see in log, that my realm#onInit method was called and
>>the realm was intialized. However, Shiro doesn’t see it. I use jetty 9 +
>>spring 5. I do NOT use spring boot. Could anyone say, how to fix it?
>>
>>--
>>Best regards, Alex Orlov