仅为控制器配置Spring拦截器

时间:2017-06-19 11:21:51

标签: java spring spring-mvc model-view-controller

我正在尝试以下列方式为控制器配置Spring拦截器。首先,我想排除以var app = angular.module('app', []); app.controller('LoginCtrl', function($scope) { $scope.isActive = false $scope.activeBtn = function () { alert("clicked!"); $scope.isActive = !$scope.isActive } });开头的所有请求。我试着用以下方式做到这一点:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<ion-header-bar>
    <div class="buttons">
        <button class="button" ng-click="closeModal()"><i class="ion-chevron-left big-icon"></i></button>
    </div>
</ion-header-bar>
<ion-content ng-app="app" ng-controller="LoginCtrl">
    <div class="modal" style="background-color: #EDEEF1;" >
        <div class="row row-white">
            <div class="col col-grey">
                <div class="divider20"></div>
            <h4 style="margin-top:30px;">Martin Nordström</h4>
            </div>
        </div>
        <div class="row row-white">
            <div class="col col-grey">
                <p style="color: #006E78; margin-bottom:40px;">990624****</p>
            </div>
        </div>
        <div class="divider20"></div>
        <div class="row row-white main-container">
            <div class="col col-grey">
                <p style="color:#006E78;">Vart vill du ta emot dina leveranser?</p>
            </div>
        </div>

        <div class="info-box" ng-class="{'info-box-active':isActive}" ng-click="activeBtn()">
            <div class="box-row">
                <div class="header">
                    <p class="leftText">IKANO Bostad</p>
                    <p class="rightText">Leveransrum</p>
                </div>
            </div>

            <div class="box-row">
                <div class="fields">
                    <p class="leftText">Folkungagatan 100</p>
                    <p class="rightText">10 kr/månad</p>
                </div>
            </div>
        </div>

        <div class="info-box">
            <div class="box-row">
                <div class="fields mixed">
                    <p class="leftText">Lägg till en ny leveransbox</p>
                    <p class="rightText">0 kr/månad</p>
                </div>
            </div>
        </div>

        <div class="row row-white">
            <div class="col col-grey">
                <button style="border-radius:50px; width:200px; height:45px;" class="button" ng-click="closeModal()">Klar</button>
            </div>
        </div>
        <div class="divider20"></div>
    </div>
</ion-content>

然而,拦截器被解雇了。哪里出错?

也许,有一个使用@ControllerAdvice的替代解决方案。但我需要获取请求标头,所以我猜它不符合我的需求。

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

Try to use "/swagger*/**" or "/swagger*" instead of "/swagger**"

答案 1 :(得分:0)

我通过以下方式解决了问题:

@ControllerAdvice
public class SomeAdvice {

    @ModelAttribute
    public void token(HttpServletRequest request) {
        // getting headers and setting the attribute in the request
        request.setAttribute("theAttribute", new SomeObject());
    }
}

然后我以这种方式在控制器中获取请求属性:

public void someMethod(@RequestAttribute("theAttribute") SomeObject someObject) {
    // some logic goes here
}

P.S。还有一点需要注意。如果您正在使用Swagger,则会遇到问题,因为Swagger会将此属性视为控制器方法参数。要忽略它,您可以使用以下配置快照:

.ignoredParameterTypes(SomeObject.class);