001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.web.jaxrs;
020
021import javax.ws.rs.core.Application;
022import javax.ws.rs.core.Feature;
023import javax.ws.rs.core.FeatureContext;
024import javax.ws.rs.ext.Provider;
025
026
027/**
028 * Shiro JAX-RS feature which includes {@link UnauthorizedExceptionExceptionMapper}, {@link SubjectPrincipalRequestFilter}, and
029 * {@link ShiroAnnotationFilterFeature}.
030 * <p>
031 * Typically, a JAX-RS {@link Application} class will include this Feature class in the
032 * classes returned from {@link Application#getClasses()} method, for example:
033 * <blockquote><pre>
034 *     public class SampleApplication extends Application {
035 *
036 *         {@code @Override}
037 *         {@code public Set<Class<?>>} getClasses() {
038 *             {@code Set<Class<?>> classes = new HashSet<Class<?>>();}
039 *
040 *             // register Shiro
041 *             classes.add(ShiroFeature.class);
042 *             ...
043 *             return classes;
044 *         }
045 *     }
046 * </pre></blockquote>
047 * NOTE: Apache CXF requires this annotation on this feature (jersey and resteasy do not)
048 *
049 * @since 1.4
050 */
051@Provider
052public class ShiroFeature implements Feature {
053
054    @Override
055    public boolean configure(FeatureContext context) {
056        context.register(UnauthenticatedExceptionExceptionMapper.class);
057        context.register(UnauthorizedExceptionExceptionMapper.class);
058        context.register(SubjectPrincipalRequestFilter.class);
059        context.register(ShiroAnnotationFilterFeature.class);
060
061        return true;
062    }
063}