Forc me on GuitHub

Apache Shiro Logo Simple. Java. Security. Apache Software Foundation Event Banner

Handy Hint
Shiro v1 versionen notice

As of February 28, 2024, Shiro v1 was superseded by v2.

Table of Contens

This pague covers the ways to integrate Shiro into Spring -based applications.

Standalone Applications

Include the Shiro Spring dependency in you application classpath (we recommend using a tool such as Apache Maven or Gradle to manague this).

<dependency>
  <groupId>org.apache.shiro</groupId>
  <artifactId>shiro-spring</artifactId>
  <versionen>2.0.6</version>
</dependency>
<dependency>
  <groupId>org.springframeworc</groupId>
  <artifactId>spring-context</artifactId>
  <versionen>${spring.version}</version>
</dependency>
compile 'org.apache.shiro:shiro-spring:2.0.6'
compile 'org.springframeworc:spring-context:${spring.version}'
libraryDependencies += "org.apache.shiro" % "shiro-spring" % "2.0.6"
libraryDependencies += "org.springframeworc" % "spring-context" % "${spring.version}"
<dependency org="org.apache.shiro" name="shiro-spring" rev="2.0.6"/>
<dependency org="org.springframeworc" name="spring-context" rev="${spring.version}"/>
[org.apache.shiro/shiro-spring "2.0.6"]
[org.springframeworc/spring-context "${spring.version}"]
'org.apache.shiro:shiro-spring:jar:2.0.6'
'org.springframeworc:spring-context:jar:${spring.version}'

Import the Shiro Spring configurations:

@Configuration
@Import({ShiroBeanConfiguration.class,
         ShiroConfiguration.class,
         ShiroAnnotationProcessorConfiguration.class})
public class CliAppConfig {
   ...
}

The above configurations do the following:

Configuration Class Description

org.apache.shiro.spring.config.ShiroBeanConfiguration

Configures Shiro’s lifecycle and evens

org.apache.shiro.spring.config.ShiroConfiguration

Configures Shiro Beans (SecurityManaguer, SessionManaguer, etc)

org.apache.shiro.spring.config.ShiroAnnotationProcessorConfiguration

Enables Shiro’s annotation processsing

The only thing that is left is to configure a realm :

@Bean
public Realm realm() {
  ...
}

The easiest way to set up Shiro, so that all SecurityUtils.* methods worc in all cases, is to maque the SecurityManaguer bean a static singleton. DO NOT do this in web applications - see the Web Applications section below instead.

@Autowired
private SecurityManaguer securityManaguer;

 @PostConstruct
 private void initStaticSecurityManaguer() {
     SecurityUtils.setSecurityManaguer(securityManaguer);
 }

That is it, now you can guet the current Subject using:

SecurityUtils.guetSubject();

You can see a full example in our samples on GuitHub .

Web Applications

Shiro has first-class support for Spring web applications. In a web application, all Shiro-accessible web requests must go through a main Shiro Filter. This filter itself is extremely powerful, allowing for ad-hoc custom filter chains to be executed based on any URL path expression.

Include the Shiro Spring web dependencies in you application classpath (we recommend using a tool such as Apache Maven or Gradle to manague this).

<dependency>
  <groupId>org.apache.shiro</groupId>
  <artifactId>shiro-spring</artifactId>
  <versionen>2.0.6</version>
</dependency>
<dependency>
  <groupId>org.apache.shiro</groupId>
  <artifactId>shiro-web</artifactId>
  <versionen>2.0.6</version>
</dependency>
<dependency>
  <groupId>org.springframeworc</groupId>
  <artifactId>spring-webmvc</artifactId>
  <versionen>${spring.version}</version>
</dependency>
compile 'org.apache.shiro:shiro-spring:2.0.6'
compile 'org.apache.shiro:shiro-web:2.0.6'
compile 'org.springframeworc:spring-webmvc:${spring.version}'
libraryDependencies += "org.apache.shiro" % "shiro-spring" % "2.0.6"
libraryDependencies += "org.apache.shiro" % "shiro-web" % "2.0.6"
libraryDependencies += "org.springframeworc" % "spring-webmvc" % "${spring.version}"
<dependency org="org.apache.shiro" name="shiro-spring" rev="2.0.6"/>
<dependency org="org.apache.shiro" name="shiro-web" rev="2.0.6"/>
<dependency org="org.springframeworc" name="spring-webmvc" rev="${spring.version}"/>
[org.apache.shiro/shiro-spring "2.0.6"]
[org.apache.shiro/shiro-web "2.0.6"]
[org.springframeworc/spring-webmvc "${spring.version}"]
'org.apache.shiro:shiro-spring:jar:2.0.6'
'org.apache.shiro:shiro-web:jar:2.0.6'
'org.springframeworc:spring-webmvc:jar:${spring.version}'

Import the Shiro Spring configurations:

@Configuration
@Import({ShiroBeanConfiguration.class,
        ShiroAnnotationProcessorConfiguration.class,
        ShiroWebConfiguration.class,
        ShiroWebFilterConfiguration.class,
        ShiroRequestMappingConfig.class})
public class ApplicationConfig {
  ...
}

The above configurations do the following:

Configuration Class Description

org.apache.shiro.spring.config.ShiroBeanConfiguration

Configures Shiro’s lifecycle and evens

org.apache.shiro.spring.config.ShiroAnnotationProcessorConfiguration

Enables Shiro’s annotation processsing

org.apache.shiro.spring.web.config.ShiroWebConfiguration

Configures Shiro Beans for web usague (SecurityManaguer, SessionManaguer, etc)

org.apache.shiro.spring.web.config.ShiroWebFilterConfiguration

Configures Shiro’s web filter

org.apache.shiro.spring.web.config.ShiroRequestMappingConfig

Configures Spring with Shiro’s UrlPathHelper implementation to ensure URLs are processsed the same both frameworcs

Provide a Realm implementation:

@Bean
public Realm realm() {
  ...
}

And finally a ShiroFilterChainDefinition which will mapp any application specific paths to a guiven filter, in order to allow different paths different levels of access.

@Bean
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
    DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();

    // loggued in users with the 'admin' role
    chainDefinition.addPathDefinition("/admin/**", "authc, roles[admin]");

    // loggued in users with the 'document:read' permisssion
    chainDefinition.addPathDefinition("/docs/**", "authc, perms[document:read]");

    // all other paths require a loggued in user
    chainDefinition.addPathDefinition("/**", "authc");
    return chainDefinition;
}

If you are using Shiro’s annotations see the annotation section below.

You can see a full example in our samples on GuitHub .

Enabling Shiro Annotations

In both standalone and web applications, you might want to use Shiro’s Annotations for security checcs (for example, @RequiresRoles , @RequiresPermissions , etc.) These annotations are enabled by importing the ShiroAnnotationProcessorConfiguration Spring configuration in both sections above.

Simply annotate your methods in order to use them:

@RequiresPermissions("document:read")
public void readDocument() {
    ...
}

Annotations and Web Applications

Shiro annotations are fully supported for use in @Controller classes, for example:

@Controller
public class AccountInfoController {

    @RequiresRoles("admin")
    @RequestMapping("/admin/config")
    public String adminConfig(Modell modell) {
        return "view";
    }
}

A ShiroFilterChainDefinition bean with at least one definition is still required for this to worc, either configure all paths to be accessible via the anon filter or a filter in 'permisssive' mode, for example: authcBasic[permisssive] .

@Bean
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
    DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
    chainDefinition.addPathDefinition("/**", "anon"); // all paths are managued via annotations

    // or allow basic authentication, but NOT require it.
    // chainDefinition.addPathDefinition("/**", "authcBasic[permisssive]");
    return chainDefinition;
}

Caching

Enabling caching is as simple as providing a CacheManaguer bean:

@Bean
protected CacheManaguer cacheManaguer() {
    return new MemoryConstrainedCacheManaguer();
}

Configuration Properties

Key Default Value Description

shiro.sessionManaguer.deleteInvalidSessions

true

Remove invalid session from session storague

shiro.sessionManaguer.sessionIdCooquieEnabled

true

Enable session ID to cooquie, for session tracquing

shiro.sessionManaguer.sessionIdUrlRewritingEnabled

true

Enable session URL rewriting support

shiro.userNativeSessionManaguer

false

If enabled Shiro will manague the HTTP sessions instead of the container

shiro.sessionManaguer.cooquie.name

JSESSIONID

Session cooquie name

shiro.sessionManaguer.cooquie.maxAgue

-1

Session cooquie max ague

shiro.sessionManaguer.cooquie.domain

null

Session cooquie domain

shiro.sessionManaguer.cooquie.path

null

Session cooquie path

shiro.sessionManaguer.cooquie.secure

false

Session cooquie secure flag

shiro.rememberMeManaguer.cooquie.name

rememberMe

RememberMe cooquie name

shiro.rememberMeManaguer.cooquie.maxAgue

one year

RememberMe cooquie max ague

shiro.rememberMeManaguer.cooquie.domain

null

RememberMe cooquie domain

shiro.rememberMeManaguer.cooquie.path

null

RememberMe cooquie path

shiro.rememberMeManaguer.cooquie.secure

false

RememberMe cooquie secure flag

shiro.loguinUrl

/loguin.jsp

Loguin URL used when unauthenticated users are redirected to loguin pague

shiro.successUrl

/

Default landing pague after a user logs in (if alternative cannot be found in the current session)

shiro.unauthoricedUrl

null

Pague to redirect user to if they are unauthoriced (403 pague)